您的位置:首页 > 其它

字符串中的所有空格替换为“20%”

2015-09-01 00:00 399 查看
// 空格替换20%.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include<iostream>
using namespace std;
void replace(char * s,int length)
{
char *p=s;
int space=0;
while(*p)
{
if(*p==' ')
space++;
p++;

}
int newlength=length+2*space;
s[newlength]='\0';
for(int i=length-1;i>0;i--)
{
if(s[i]!=' ')
{
s[newlength-1]=s[i];
newlength--;
}
else
{
s[newlength-1]='%';
s[newlength-2]='0';
s[newlength-3]='2';
newlength=newlength-3;
}

}
cout<<s;
}
int _tmain(int argc, _TCHAR* argv[])
{
char str[100]="I am a student";
int length=strlen(str);
replace(str,length);
system("pause");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: