POJ3981 字符串替换 C语言

题目:http://poj.org/problem?id=3981

思路:搜索到"you"就替换成"we", 否则原样复制

提交情况:AC 1次

 

AC code:

View Code
 1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #define MAXN (1000 + 20)
5
6 char s[MAXN], e[MAXN];
7
8 void Replace() {
9 char ch[5];
10 int i, j = 0;
11 int len = strlen(s); //长度
12 if(len < 3) {
13 strcpy(e, s);
14 return;
15 }
16 else {
17 for(i = 0; i < len; ) {
18 if(s[i] == 'y' && s[i + 1] == 'o' && s[i + 2] == 'u') { //出现you
19 e[j++] = 'w';
20 e[j++] = 'e';
21 i += 3; //替换,i指向'u'后字符
22 }
23 else {
24 e[j++] = s[i++]; //原样复制
25 }
26 }
27 e[j] = '\0'; //加结束符
28 }
29 }
30
31 int main() {
32 while(gets(s) != NULL) { //结束返回NULL
33 Replace();
34 printf("%s\n", e);
35 }
36 return 0;
37 }

posted @ 2011-07-24 15:10  cloehui  阅读(323)  评论(0编辑  收藏  举报