求一个字符串中出现次数最多的子串
2015-06-08 15:53
148 查看
#include <stdio.h> #include <string.h> void func(char *pstr, char *sstr); int count_func(char *p, char *s); int main() { char *data0 = "aaabbbbcccccccc'"; char data1[6]; func(data1,data0); printf("%s\n",data1); return 0; } void func(char *pstr, char *sstr) { int len = (int)strlen(sstr); int count; char tem[10]; int num0 = 0; int num1 = 0; char tempory[10]; int cur, i, p; for(count = 1; count<=len/2; count++)//the length of string { for(cur = 0; cur+count<=len; cur++) { /***************************************************************************/ for(i=0; i<count; i++)//把要对比的字符串保存到tempory[]中 { tempory[i] = sstr[cur+i]; } tempory[i] = '\0'; num0 = count_func(sstr,tempory);//统计在字符串中字符串的个数 if(num0>num1)//如果大于原先的,则改变 { num1 = num0; strcpy(tem,tempory); for(p=0;tem[p]!='\0';p++) { pstr[p] = tem[p]; } pstr[p] ='\0'; } } } } int count_func(char *p, char *s) { int i, j; int num=0; for(i=0; p[i]!='\0'; i++) { for(j=0;s[j]!='\0';j++) { if(p[i+j]!=s[j]) break; } if(s[j]=='\0') num++; } return num; }
相关文章推荐
- py plistlib
- App 打包的几种方式
- 第十四周项目4(3)-处理C++源代码的程序
- csapp lab3 bufbomb 缓存区溢出攻击 《深入理解计算机系统》
- self和super的区别
- 央视公然诽谤Linux操作系统,谁报告?
- 文件上传利器SWFUpload入门简易教程
- c++ --> static关键字总结
- 焦距
- Mac 安装brew
- [Unity3D]蓝港面试题
- Android面试宝典
- 傅立叶变换的通俗入门
- 模板类的嵌套
- mac上连接魅族手机调试,不显示device
- java设计模式(四)---原型模式
- C#基础知识
- 阅读笔记之《你的灯亮着吗》第一,二章
- android:padding和android:layout_margin的区别?
- 读书笔记--<精益和敏捷开发大型项目应用指南>