/*测试数据:Shen zhen is a beautiful city!*/ /*运行结果:Shen%20zhen%20is%20a%20beautiful%20city!*/ #include<stdio.h> #define SIZE 1000 void wordReplace(char *str) { char *p = str, *e=str,*q,*r; char rp[] = "%20"; r = rp; while (*e != 0){ ++e; } q=e; while (*p){ while (*p == 32){ while (q != p){ *(q + 2) = *q;/*之所以是2,是因为%20有三个字符,由于当前字符本身为空格,所以只需要往后面移动两位即可*/ --q; } while (*r != 0){/*将%20字符串复制到空白区域*/ *p = *r; ++p; ++r; } --p;/*为了能用下面的++p*,先将p往前移动一位/ r = rp;/*初始化*/ e += 2;/*字符串长度增加2*/ q = e;/*初始化*/ } ++p; } puts(str);/*输出改动后的字符串*/ } int main() { char str[SIZE]; printf("Please enter a string :\n"); gets(str); //wordCount(str); //wordReplace(str); reverseWord(str); return 0; }
原文地址:http://blog.csdn.net/nyist327/article/details/28605037