编写一个实现字符串替换的程序,
1: 题目
健写一个实现字符串替换的程序,该程序必须似含函数 replace(char*s,char*t,char“u),该函数实现将字符串s中所有出现的子串t替换为串u,并
输出替换后的串及替换的子串个数。若s中不包含子串t,则输出提示信息:没有满足条件的子串t。例如:replace("java programming","java","c");将输出
e programming,1":replace("java programming","python","c");将输出“没有满足条件的子串python"。main函数主要实现从键盘输入s、t、u这三个字符串,
并实现对replace函数的调用。
实现:
#include <stdio.h> #include <string.h> int k=0; char *strrpc(char *str,char *oldstr,char *newstr){ char bstr[strlen(str)]; memset(bstr,0,sizeof(bstr)); for(int i = 0;i < strlen(str);i++){ if(!strncmp(str+i,oldstr,strlen(oldstr))){ strcat(bstr,newstr); i += strlen(oldstr) - 1; k++; }else{ strncat(bstr,str + i,1); } } strcpy(str,bstr); return str; } int main(void) { char s[100]; char t[100]; char u[100]; char copy[100]; scanf("%s%s%s",s,t,u); strcpy(s, copy); //strrpc(str,"java","c"); strrpc(s,t,u); if(strcmp(s,copy)==0){ printf("没有满足条件的%s",t); }else{ printf("%s %d",s,k); } return 0; }
相关推荐
lynjay 2020-06-14
88384957 2020-06-12
zuihaobushi 2020-04-30
xiaohouye 2020-04-20
菇星獨行 2020-04-20
qscool 2020-04-16
Joymine 2020-03-04
CosEmon 2020-03-01
GoatSucker 2020-02-15
Joymine 2020-02-11
twater000 2020-02-01
AaronPlay 2020-06-13
一只码畜 2020-01-28
ILVNMM 2020-10-26
PinkBean 2020-08-19
Seandba 2020-08-16
徐建岗网络管理 2020-07-28
herohope 2020-06-10
adwen00 2020-06-09