[蓝蓝计算机考研算法练习二期] day12

74 阅读1分钟

【蓝蓝计算机考研算法练习二期】- day12 -字符串转换

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

输入:包含多行数据每行数据是一个字符串,长度不超过1000,数据以EOF结束
输出:对于输入的每一行,输出替换后的字符串

示例

输入:you love we
输出:we love we

思路

感觉就是遍历,然后判断,之后再替换

具体实现

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
	char str[1000]={'0'};
	gets(str);
	int i=strlen(str);
	for (int j=0;j<i;j++) {
        if (str[j]=='y'&&str[j+1]=='o'&&str[j+2]=='u'){
            str[j]='w';
            str[j+1]='e';
            for(;j+3<i+1;j++){
                str[j+2]=str[j+3];
			} 
        }
   	}
    puts(str);
}

image.png

ps:代码写的很烂