Open In App

URLify a given string (Replace spaces with %20)

Last Updated : 24 Apr, 2025
Suggest changes
Share
Like Article
Like
Report

Given a string s, the task is to replace all the spaces in the string with '%20'.

Examples: 

Input: s = "i love programming"
Output: "i%20love%20programming"
Explanation: The 2 spaces are replaced by '%20'

Input: s = "ab cd"
Output: "ab%20cd"

Approach:

The idea is to iterate through each character of the input string and create a new string by replacing space characters with the '%20' escape sequence.

Step by step approach:

  1. Iterate through each character in the input string from start to end.
  2. Check if the current character is a space.
    • If a space is found, append '%20' to the result string.
    • If the current character is not a space, append the original character to the result string.
  3. Return the final modified string with spaces replaced by '%20'.
C++
// C++ program to URLify a given string #include <bits/stdc++.h> using namespace std; string URLify(string& s) {  int n = s.length();  string ans = ""; for (int i = 0; i<n; i++) {    // If " " is encountered,   // append "%20" to string if (s[i] == ' ') {  ans += "%20"; }  // Else append the current  // character. else {  ans += s[i]; } } return ans; } int main() { string s = "i love programming"; cout << URLify(s); return 0; } 
Java
// Java program to URLify a given string class GfG {  static String URLify(String s) {  int n = s.length();    StringBuilder ans = new StringBuilder();  for (int i = 0; i < n; i++) {    // If " " is encountered,   // append "%20" to string  if (s.charAt(i) == ' ') {  ans.append("%20");  }    // Else append the current   // character.  else {  ans.append(s.charAt(i));  }  }  return ans.toString();  }  public static void main(String[] args) {  String s = "i love programming";  System.out.println(URLify(s));  } } 
Python
# Python program to URLify a given string def URLify(s): n = len(s) ans = "" for i in range(n): # If " " is encountered,  # append "%20" to string if s[i] == ' ': ans += "%20" # Else append the current  # character. else: ans += s[i] return ans if __name__ == "__main__": s = "i love programming" print(URLify(s)) 
C#
// C# program to URLify a given string using System.Text; using System; class GfG {  static string URLify(string s) {  int n = s.Length;    StringBuilder ans = new StringBuilder();  for (int i = 0; i < n; i++) {    // If " " is encountered,   // append "%20" to string  if (s[i] == ' ') {  ans.Append("%20");  }    // Else append the current   // character.  else {  ans.Append(s[i]);  }  }  return ans.ToString();  }  static void Main() {  string s = "i love programming";  Console.WriteLine(URLify(s));  } } 
JavaScript
// JavaScript program to URLify a given string function URLify(s) {  let n = s.length;    let ans = "";  for (let i = 0; i < n; i++) {    // If " " is encountered,   // append "%20" to string  if (s[i] === ' ') {  ans += "%20";  }    // Else append the current   // character.  else {  ans += s[i];  }  }  return ans; } let s = "i love programming"; console.log(URLify(s)); 

Output
i%20love%20programming

Time Complexity: O(n), as the string is traversed only once.
Auxiliary Space: O(n), used for output string.


Similar Reads

Article Tags :