// 空格替換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; }