替换空格

来源:互联网 发布:卖家如何加入农村淘宝 编辑:程序博客网 时间:2024/06/11 15:30

题目:请实现一个函数,把字符串中每个空格替换成"%20"。例如输入“We are happy.”,则输出"We%20are%20happy."。

#include "iostream"#include "string"using namespace std;#define MAXSIZE100void ReplaceBlank(char* str){int len,length,count =0,index =0;char* p1,*p2;while(str[index] !='\0'){if (str[index] ==' '){count++;}index++;}length =index+count*2;p2 =str +length;p1 =str+strlen(str);while(p1!=str){if (*p1 ==' '){p1--;*(p2--) ='0';*(p2--) ='2';*(p2--) ='%';}else{*(p2--) =*(p1--);}}if (*p1 ==' '){*(p2--) ='0';*(p2--) ='2';*(p2--) ='%';}}void main(){char array[MAXSIZE] ={"We are happy!  huang tao!"};ReplaceBlank(array);cout<<array<<endl;}



0 0
原创粉丝点击