POJ 3981(字符串替换)

简介: 字符串替换 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7290   Accepted: 3451 Description 编写一个C程序实现将字符串中的所有"you"替换成"w...
字符串替换
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; 
}

 

目录
相关文章
|
5月前
PTA-第3章-13 字符串替换
编写程序,将字符串中大写字母按A-&gt;Z, B-&gt;Y, ..., X-&gt;C, Y-&gt;B, Z-&gt;A的规则替换。输入为不超过80字符的字符串,输出替换后的字符串。例如,&quot;Only the 11 CAPItaL LeTtERS are replaced.&quot; 转换为 &quot;Lnly the 11 XZKRtaO OeGtVIH are replaced.&quot;
54 1
|
9天前
AcWing 831. KMP字符串
AcWing 831. KMP字符串
7 0
代码随想录 Day7 字符串1 LeetCode T344反转字符串 T541 反转字符串II 151翻转字符串的单词
代码随想录 Day7 字符串1 LeetCode T344反转字符串 T541 反转字符串II 151翻转字符串的单词
40 0
|
5月前
|
索引
《华为机试》——查找两个字符串a,b中的最长公共子串
《华为机试》——查找两个字符串a,b中的最长公共子串
华为机试HJ65:查找两个字符串a,b中的最长公共子串
华为机试HJ65:查找两个字符串a,b中的最长公共子串
华为机试HJ81:字符串字符匹配
华为机试HJ81:字符串字符匹配
|
算法 C++
剑指offer(C++)-JZ19:正则表达式匹配(算法-动态规划)
剑指offer(C++)-JZ19:正则表达式匹配(算法-动态规划)
华为机试HJ12:字符串反转
华为机试HJ12:字符串反转
AcWing 765. 字符串加空格
AcWing 765. 字符串加空格
53 0
AcWing 765. 字符串加空格
AcWing 770. 单词替换
AcWing 770. 单词替换
52 0
AcWing 770. 单词替换