输入一个正整数 repeat (0<repeat<10),做 repeat 次下列运算:
输入一个以回车符结束的字符串,再输入一个字符,统计并
输出该字符在字符串中出现的次数。
输入输出示例:括号内为说明
输入:
2
(repeat=2)
phoebe
(字符串"phoebe")
e
(字符'e')
1234
(字符串"1234")
a
(字符'a')
输出:
count = 2
('e'在"phoebe"中出现 2 次)
count = 0 ('a'在"1234"中没有出现过)
#include<stdio.h>
#define N 999
int main(void)
{
char t, ch;
char str[N];
int i=0,count=0;
int repeat,x;
scanf("%d", &repeat);
getchar();
for(x= 1;x<= repeat;x++){
i=0;
while((str[i] = getchar( )) != '\n')
i++;
str[i] = '\0';
t= getchar();
getchar();
count=0;
for(i=0;str[i] !=0;i++){
if(str[i]==t) count++;
}
printf("count = %d\n", count);
}
}
❤一定要记住i=0和count=0的说明。最后一个for语句 str[i]!=0x等价于str[i] !='\0'。
呆呆的学妹求点赞捏❤