Replace String
/***
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.
***/
#include<iostream>
#include<string>
using namespace std;
void replaceString(string *str) {
string::iterator it;
string str1 = "%20";
for(it = str->begin(); it != str->end(); it++) {
if(*it == ' ') {
str->replace(it, it+1, str1);
}
}
}
Test
void main() {
string str = "Xiang is a joke.";
replaceString(&str);
cout<<str<<endl;
}
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.
***/
#include<iostream>
#include<string>
using namespace std;
void replaceString(string *str) {
string::iterator it;
string str1 = "%20";
for(it = str->begin(); it != str->end(); it++) {
if(*it == ' ') {
str->replace(it, it+1, str1);
}
}
}
Test
void main() {
string str = "Xiang is a joke.";
replaceString(&str);
cout<<str<<endl;
}
Comments
Post a Comment