c++利用字符串处理c字符数组替换空格
替换空格
http://www.nowcoder.com/questionTerminal/4060ac7e3e404ad1a894ef3e17650423
include
include
include
include
using namespace std;
class Solution {
public:
void replaceSpace(char str,int length) {
string str1(str,length);//将c字符串赋值给字符串对象
for(int i=0;i<str1.size();i++)
{
if(str1[i]==' ')
{
str1.replace(i,1,"%20");//调用字符串类方法replace
length=length+2; //统计c字符数组增加后的长度
}
}
str=(char)realloc(str,length);//为c字符数组重新开辟空间
for(int i=0;i<str1.size();i++)
{
str[i]=str1[i]; //将字符串赋值给c字符数组
}
str[length-1]='\0'; //添加\0
return;
}
};


