POJ 3981(字符串替换)

字符串替换
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 7290   Accepted: 3451

Description

编写一个C程序实现将字符串中的所有"you"替换成"we"

Input

输入包含多行数据

每行数据是一个字符串,长度不超过1000
数据以EOF结束

Output

对于输入的每一行,输出替换后的字符串

Sample Input

you are what you do

Sample Output

we are what we do
第一:
#include<stdio.h> 
char str[1002]; 
int main() 
{ 
  int i; 
  while(gets(str)!=NULL) 
  { 
    i=0;
   // while(str[i]!='\0')
   for(i=0;str[i]!='\0';i++) 
    if(str[i]=='y'&&str[i+1]=='o'&&str[i+2]=='u') 
    { 
      printf("we"); 
      i+=2; 
    } 
    else
     {   
        printf("%c",str[i]); 
        //i++; 
     } 
     printf("\0");
    printf("\n");
  } 
  return 0; 
}
第二: 
#include<stdio.h> 
#include<string.h>
char str[1002]; 
int main() 
{ 
  int i; 
  while(gets(str)!=NULL) 
  { 
    i=0;
    while(str[i]!='\0') 
    if(str[i]=='y'&&str[i+1]=='o'&&str[i+2]=='u') //短路,可以写道str[i+2] 
    { 
      printf("we"); 
      i+=3; 
      //printf("%d\n",i);
    } 
    else
    {   
        printf("%c",str[i]); 
        i++; 
    }
    printf("\0"); //必须是双引号 
    printf("\n");
  } 
  return 0; 
}

 

posted @ 2012-07-30 08:42  加拿大小哥哥  阅读(740)  评论(0编辑  收藏  举报