字符串子串的个数

来源:互联网 发布:中国汽车历年进口数据 编辑:程序博客网 时间:2024/04/28 20:19

“Think Different”曾经是美国苹果公司的广告口号,小明非常喜欢这句话,并且把它当作自己的座右铭。一天小红想测试一下小明对这句话到底有多喜欢,于是小红写了一个很长的字符串给小明,问他这个字符串里面有几个子串是这句话。
不过小红一向喜欢难为小明,所以设置了以下规定:
符合条件的子串的形式为:think+一个空格+different,其中两个单词之间的空格必须有,并且有且仅有一个,同时两个单词中的任何一个字母大小写均可,都符合要求。
例如,“Think Different”、“think different”、“THINK DIFFERENT”都是符合要求的子串。

输入格式

输入包含多组测试数据。
每组输入一个字符串s,字符串长度不超过100。

输出

对于每组输入,输出有几个符合要求的子串。

样例输入

think different
THINKdifferENTandTHink diFFerent
Think Different
i Like think DIFFERENT you like think different

样例输出

1
1
1
2

AC代码:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int count_strstr(char *str,char *substr)
{
    int count=0;
    char *pos = str; 
    
    while(pos = strstr(pos,substr))
    {
        ++count;
        ++pos;
    }    
    return count;
}    


int main()
{
char s[101];
int num,m,i;
while(gets(s))
{
m=strlen(s);
for(i=0;i<m;i++)
{
if(s[i]>='A'&&s[i]<='Z')
{
s[i]+=32;
}
}
num=0;
num=count_strstr(s,"think different");
printf("%d\n",num);
}
   return 0;
}

原创粉丝点击