题解 | #单词替换#

单词替换

https://www.nowcoder.com/practice/5b58a04679d5419caf62c2b238e5c9c7

#include <stdio.h>
#include<string.h>
int main() {
	char str1[100];//字符串
	char str2[10];//旧单词
	char str3[10];//新单词
	while (gets(str1) != NULL) { 
		gets(str2);
		gets(str3);
		char temp[100];
		memset(temp, 0, sizeof(char));//初始化temp数组;
		char *p;
		for (p = strtok(str1, " "); p != NULL; p = strtok(NULL, " ")) {//利用strtook分割空格,每次分割一个单词,用p指针保存
			if (strcmp(p, str2) == 0) {	//若与原单词相等,则新单词写入temp,并加上一个空格字符
				strcat(temp, str3);
				strcat(temp, " \0");
			}
			else {	//若与原单词不匹配,不改变单词写入temp,并加上一个空格字符
				strcat(temp, p);
				strcat(temp, " \0");
			}
		}
		printf("%s\n", temp);//打印字符
	}
	return 0;
}

全部评论

相关推荐

活泼的代码渣渣在泡池...:哈哈哈挺好的,我也上岸美团了,不说了,我又接了一单
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务