Run ID 作者 问题 语言 测评结果 时间 内存 代码长度 提交时间
32011 yejiaxiangBMT 拼接字符串 C++ 运行出错 0 MS 192 KB 1636 2023-11-24 16:07:38

Tests(0/1):


#include "string.h" #include "stdio.h" void str_replace(char * cp, int n, char * str) { int lenofstr; int i; char * tmp; lenofstr = strlen(str); //str3比str2短,往前移动 if(lenofstr < n) { tmp = cp+n; while(*tmp) { *(tmp-(n-lenofstr)) = *tmp; //n-lenofstr是移动的距离 tmp++; } *(tmp-(n-lenofstr)) = *tmp; //move '\0' } else //str3比str2长,往后移动 if(lenofstr > n) { tmp = cp; while(*tmp) tmp++; while(tmp>=cp+n) { *(tmp+(lenofstr-n)) = *tmp; tmp--; } } strncpy(cp,str,lenofstr); } int main() { char str1[1024]; char str2[100],str3[100]; int i,len,count=0; char c; char *p; printf("\n请输入要查找的字符串和用于替换的字符串(中间用空格隔开): "); scanf("%s",str2); scanf("%s",str3); //read string from news.txt freopen("news.txt","r",stdin); i=0; c = getchar(); while(c!=EOF) { str1[i] = c; i++; c = getchar(); } str1[i] = '\0'; //开始查找字符串str2 p = strstr(str1,str2); while(p) { count++; //每找到一个str2,就用str3来替换 str_replace(p,strlen(str2),str3); p = p+strlen(str3); p = strstr(p,str2); } printf("\ncount = %d\n",count); printf("Result = %s\n",str1); }


测评信息: