代码艺术家
Code artist

很喜欢释迦牟尼佛的一句话:“无论你遇见谁,他都是你生命该出现的人,绝非偶然,他一定教会你一些什么”。

有问题 问我 问Google

删除重复子串

 1 #include <iostream>
 2 #include <stdio.h>
 3 using namespace std;
 4 int main(int argc, char *argv[])
 5 {
 6     char str1[50],str2[50],str3[50],*p1,*p2,*p3;
 7     int num=0;
 8     cout<<"输入两个子串\n";
 9     gets(str1);
10     gets(str2);
11     p1=str1;
12     p2=str2;
13     p3=str3;
14     while(*p1!='\0')
15     {
16         if(*p1==*p2)
17         {
18             while(*p1==*p2&&*p2!='\0')
19             {
20                 p1++;
21                 p2++;
22             }
23         }
24         else {
25     //    cout<<*p1<<endl;
26         *p3=*p1;
27         p1++;
28         p3++;
29         }
30     
31         if(*p2=='\0')
32         num++;
33         p2=str2;                 //p2重新指向str2,然后通过循环接着找 
34     }
35             *p3='\0';        //关键部分,不然输出的字符数组是不完整的        
36     cout<<"子串个数:"<<num<<endl; 
37     cout<<"删除重复子串后:"<<str3<<endl;
38     
39     return 0;
40 }    

 

posted @ 2015-04-24 00:58  Jason‘  阅读(297)  评论(0编辑  收藏  举报