字符串连接的几种实现

利用系统函数:

# include <string.h>
# include <stdio.h>
int main()
{
    char a[100],b[100];
    while(gets(a)!=NULL)
    {
        gets(b);
        char * flat=strstr(a,b);
        if(!flat)//���NULL��no
            printf("NO\n");
        else
            printf("YES\n");
    }
    return 0;
}


BF(Brute-Force,布鲁斯-福斯)算法

 1 # include <stdio.h>
 2 # include <string.h>
 3 typedef struct node //抽象化定义;
 4 {
 5     char data[100];
 6     int len;
 7 }sqstring;
 8 int Index(sqstring s,sqstring t)
 9 {
10     
11     int i=0;
12     int j=0;
13     while(i<s.len && j<t.len)
14     {
15         if(s.data[i]==t.data[j])
16         {
17             i++;j++; //从模式串开始逐一进行比较;
18         }
19         else 
20         {
21             i=i-j+1;j=0; //如果出现不匹配,则模式串返回到初始值,
22             //主串返回到比较完的后一个位置
23         }
24     }
25     if(j>=t.len)
26         return i-t.len;//返回模式串在主串中的位置。
27     else 
28         return -1;
29     
30 }
31 int main()
32 {
33     sqstring str1,str2;
34     int l;
35     while(gets(str1.data) != NULL)
36     {
37         gets(str2.data);
38         str1.len=strlen(str1.data);
39         str2.len=strlen(str2.data);
40         l=Index(str1,str2);
41         if(l!=-1)
42             printf("YES\n");
43         else 
44             printf("NO\n");
45     }
46     return 0;
47 }

 

posted on 2013-10-10 10:59  随风浪子的博客  阅读(299)  评论(0编辑  收藏  举报

导航