URLify a given string (Replace spaces with %20)
Last Updated :
24 Apr, 2025
Improve
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"
Try it on GfG Practice
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:
- Iterate through each character in the input string from start to end.
- 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.
- Return the final modified string with spaces replaced by '%20'.
// 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;
}
31
1
// C++ program to URLify a given string
2
3
using namespace std;
4
5
string URLify(string& s) {
6
int n = s.length();
7
8
string ans = "";
9
for (int i = 0; i<n; i++) {
10
11
// If " " is encountered,
12
// append "%20" to string
13
if (s[i] == ' ') {
14
ans += "%20";
15
}
16
17
// Else append the current
18
// character.
19
else {
20
ans += s[i];
21
}
22
}
23
24
return ans;
25
}
26
27
int main() {
28
string s = "i love programming";
29
cout << URLify(s);
30
return 0;
31
}
// 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 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# 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 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.