14、字符匹配,输出出现的位置

 1 //1、字符串s1,s2
 2 //2、s2[i]取出
 3 //3、s1自0递增,连续和s2比较
 4 //4、匹配到相同,比较停止,返回2,取出s2[i+1],循环开始
 5 //5、没有匹配到相同,比较停止,返回2,取出s2[i+1],循环开始
 6 
 7 #include
 8 #define Maxlength 1000
 9 
10 int any(char s1[], char s2[]);
11 
12 int main()
13 {
14     int c;
15     int i = 0;
16     int result = 0;
17 
18     char s1[Maxlength];
19     char s2[Maxlength];
20 
21     for(i=0; ii)
22         s1[i] = '\0';
23     for(i=0; ii)
24         s2[i] = '\0';
25 
26     printf("请输入字符串1:\n");
27     for(i=0; (c=getchar())!='\n'; ++i)
28         s1[i] = c;
29 
30     printf("请输入字符串2:\n");
31     for(i=0; (c=getchar())!='\n'; ++i)
32         s2[i] = c;
33 
34     result = any(s1, s2);
35     if(result == 0)
36         printf("\n未查询到匹配部分!");
37     else if(result == 1)
38         printf("\n匹配完成!");
39     else if(result == 2)
40         printf("\n字符串1未输入有效内容!");
41     else if(result == 3)
42         printf("\n字符串2未输入有效内容!");
43 
44     return 0;
45 }
46 
47 int any(char s1[], char s2[])
48 {
49     int i = 0;      //s2
50     int p = 0;      //s1
51     int state = 0;  //单个匹配状态
52     int m = 0;      //匹配结果,0:无相同部分 1:匹配到内容 2:S1未输入 3:s2未输入
53 
54     if(s1[i] == '\0' || s1[i] == '\n')
55         m = 2;
56     if(s2[i] == '\0' || s2[i] == '\n')
57         m = 3;
58 
59     for(i=0; s2[i]!='\0'; ++i,state=0,p=0)
60     {
61         while(state==0 && s1[p]!='\0')
62         {
63             if(s1[p] == s2[i])
64             {
65                 printf("%d", p+1);
66                 state = 1;
67                 m = 1;
68                 ++p;
69             }
70             else if(s1[p+1] == '\0')
71             {
72                 putchar('|');
73                 ++p;
74             }
75             else
76                 ++p;
77         }
78     }
79     return m;
80 }

14、字符匹配,输出出现的位置_第1张图片

 

 1 //1、字符串s1,s2
 2 //2、s2[i]取出
 3 //3、s1自0递增,连续和s2比较
 4 //4、匹配到相同,比较停止,返回2,取出s2[i+1],循环开始
 5 //5、没有匹配到相同,比较停止,返回2,取出s2[i+1],循环开始
 6 
 7 #include
 8 #define Maxlength 1000
 9 
10 int any(char s1[], char s2[]);
11 
12 int main()
13 {
14     int c;
15     int i = 0;
16     int result = 0;
17 
18     char s1[Maxlength];
19     char s2[Maxlength];
20 
21     for(i=0; ii)
22         s1[i] = '\0';
23     for(i=0; ii)
24         s2[i] = '\0';
25 
26     printf("请输入字符串1:\n");
27     for(i=0; (c=getchar())!='\n'; ++i)
28         s1[i] = c;
29 
30     printf("请输入字符串2:\n");
31     for(i=0; (c=getchar())!='\n'; ++i)
32         s2[i] = c;
33 
34     result = any(s1, s2);
35     if(result == 0)
36         printf("\n未查询到匹配部分!");
37     else if(result == 1)
38         printf("\n匹配完成!");
39     else if(result == 2)
40         printf("\n字符串1未输入有效内容!");
41     else if(result == 3)
42         printf("\n字符串2未输入有效内容!");
43 
44     return 0;
45 }
46 
47 int any(char s1[], char s2[])
48 {
49     int i = 0;      //s2
50     int p = 0;      //s1
51     int state = 0;  //单个匹配状态
52     int m = 0;      //匹配结果,0:无相同部分 1:匹配到内容 2:S1未输入 3:s2未输入
53 
54     if(s1[i] == '\0' || s1[i] == '\n')
55         m = 2;
56     if(s2[i] == '\0' || s2[i] == '\n')
57         m = 3;
58 
59     for(i=0; s2[i]!='\0'; ++i,state=0,p=0)
60     {
61         while(state==0 && s1[p]!='\0')
62         {
63             if(s1[p] == s2[i])
64             {
65                 putchar(s2[i]);
66                 putchar('\t');
67                 printf("%d\n", p+1);
68                 state = 1;
69                 m = 1;
70                 ++p;
71             }
72             else if(s1[p+1] == '\0')
73             {
74                 putchar(s2[i]);
75                 putchar('\t');
76                 putchar('|');
77                 putchar('\n');
78                 ++p;
79             }
80             else
81                 ++p;
82         }
83     }
84     return m;
85 }

14、字符匹配,输出出现的位置_第2张图片

 

转载于:https://www.cnblogs.com/Gozz/p/7912052.html

你可能感兴趣的:(14、字符匹配,输出出现的位置)