White space replace

Write a method to replace all spaces in a string with ‘%20’. You may assume that the string has sufficient space at the end of the string to hold the additional characters, and that you are given the “true” length of the string. 


Input: “Mr John Smith        “

Output: “Mr%20John%20Smith”

01 void stringProcess(char* s, int k)
02 {
03     char* t=new char[k];
04     for(int i=0;i<k;i++)
05         t[i]=s[i];
06     int index=0;
07     for(int i=0;i<k;i++){
08         if(t[i]==‘ ‘){
09             s[index++]=‘%’;
10             s[index++]=‘2’;
11             s[index++]=‘0’;
12         }
13         else
14             s[index++]=t[i];
15     }
16 }
This entry was posted in String. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s