皮卡丘的叫声是? 字符串里某个字符的数量
皮卡丘的叫声是?
Problem Description
皮卡丘的叫声大家听过吗?当然是“pika”,“pikapi”,“pikachu”,“pikapika”等等,其中“pikapi”指的是小智哦(不要问我为什么懂皮卡丘语),现在皮卡丘说了好长一段话,请你找出皮卡丘叫了多少次“pikachu”。
Input
输入数据有多组,到EOF结束。
每组数据输入一段皮卡丘的叫声,叫声用字符串来表示,字符串中含逗号,不含空格,长度不超过1000。
Output
对于每组输入,输出一个整数n,表示叫声中“pikachu”的数量。
Example Input
pika,pikachu pikapika,chupi pikachu,pikapi,pipikachu
Example Output
1 0 2
法一:
#include<stdio.h>
#include<string.h>
int main(void)
{
char str[1002], count;
int i, n;
while(gets(str))
{
n = strlen(str);
count = 0;
for(i = 0; i < n - 5; i++)
{
if(str[i] == 'p')
{
if(str[i + 1] == 'i')
{
if(str[i + 2] == 'k')
{
if(str[i + 3] == 'a')
{
if(str[i + 4] == 'c')
{
if(str[i + 5] == 'h')
{
if(str[i + 6] == 'u')
{
count++;
}
}
}
}
}
}
}
}
printf("%d\n", count);
}
return 0;
}
法二:
#include<stdio.h>
#include<string.h>
int main(void)
{
char str[1002], s[8] = "pikachu";
int i, n, k, j, count;
while(gets(str))
{
n = strlen(str);
k = 0;
count = 0;
for(i = 0; i < n; i++)
{
if(str[i] == s[0])
{
for(j = i + 1, k = 1; k <= 6 && j <= n - 1; k++, j++) 或 for(j = i + 1, k = 1; k <= 6 && j <= n - 1; ++k, ++j)
{
if(str[j] != s[k])
{
break;
}
}
if(k == 7) 因为当k == 7 时 刚好不符合 条件, 跳出 for 循环
{
count++;
}
}
}
printf("%d\n", count);
}
return 0;
}