字符串处理系统.docVIP

  • 31
  • 0
  • 约4.24千字
  • 约 5页
  • 2016-02-25 发布于江苏
  • 举报
字符串处理系统.doc

#includestdio.h #includestdlib.h #includestring.h #define M 10 #define N 200 void zcgstj() { system(cls); printf(\n\t\t\t你正在使用子串个数统计的功能,按Enter键继续!!); getchar(); getchar(); char str1[20],str2[20],*p1,*p2; int sum=0; printf(please input a father strings\n); printf(please input a son strings\n); scanf(%s%s,str1,str2); p1=str1; p2=str2; while(*p1!=\0) {if(*p1==*p2) {while(*p1==*p2*p2!=\0) {p1++; p2++; } } else p1++; if(*p2==\0)sum++; p2=str2; } printf(%d,sum); getchar(); system(pause); } void zfcczqth() { system(cls); printf(\n\t\t\t你正在使用字符串查找且替换的功能,按Enter键继续!!); getchar(); getchar(); int i=0,j,k=0,m=0,pos=0,max,len1,len2; char s[N],t[N],a[200],s1[200],s2[200]; FILE *fp; printf(Please input file name:\n);/*输入文件名*/ gets(a); printf(Please input original string:\n);/*输入要替换的字符串*/ gets(s1); printf(Please input new string:\n);/*输入被替换成的字符串*/ gets(s2); len1=strlen(s1); len2=strlen(s2); if ((fp=fopen(g:\\xyz\\1.txt,r))== NULL)/*设定文件位于当前目录下,可更改为绝对路径*/ { printf(Open file error! Strike any key to exit!,g:\\xyz\\1.txt); system(pause); exit(1); } s[i++]=fgetc(fp); while(!feof(fp)) { s[i++]=fgetc(fp); } fclose(fp); max=i-1;/* 函数feof()最后会读两次,所以必须减1 */ for(i=0;imax-len1;i++) { for(j=0;jlen1(s[i+j]==s1[j]);j++); if(j!=len1) continue;/* 不相等则i加1进行下一次外循环 */ else { for(k=0;ki-pos;k++)/* pos记录开始复制的位置 */ t[m++]=s[pos+k]; pos=i+len1; for(k=0;klen2;k++)/* 字符串替换 */ t[m++]=s2[k]; } } for(k=pos;kmax;k++)/* 复制剩余字符 */ t[m++]=s[k];max=m;/* max是替换后文件的字符总数 */ fp=fopen(g:\\xyz\\1.txt,w); printf(\n\nThe results are:\n\n); for(j=0;jmax;j++) {putchar(t[j]); /* 结果显示到屏幕 */ fputc(t[j],fp);/* 同时结果写入到当前目录下的新文件001.txt中 */ } printf(\n\n); fclose(fp); system(pause); } void sczc() { system(cls); printf(\n\t\t\t你正在使用删除子串的功能,按Enter键继续!!); getchar(); getchar(); char a[15]; char b[15]; char *s; char *p=a; char *q=b; int i; printf(please input a father strings\n); printf(please input a son strings\n);

文档评论(0)

1亿VIP精品文档

相关文档