统计关键字个数从键盘读入一组字符(
来源:学生作业帮助网 编辑:六六作业网 时间:2024/11/17 01:49:02
统计关键字个数从键盘读入一组字符(
统计关键字个数
从键盘读入一组字符(
统计关键字个数从键盘读入一组字符(
#include
#include
int main()
{
char str[1001] = {0};
char keyword[5][20] = {0};
int n;
int i;
int count(char str[], char keyword[]);
printf("Input the string: ");
gets(str);//输入字符串
printf("How many Keyword do you want to check: ");
scanf("%d", &n);//输入关键字个数
gets(keyword[0]);//因为scanf的问题这里需要将scanf遗留的字符读取完毕
keyword[0][0] = 0;
for (i = 0; i < n; i++)
{
printf("Input keyword%d: ", i + 1);
gets(keyword[i]);
}
printf("\n");
for (i = 0; i < n; i++)
{
printf("%s: %d\n", keyword[i], count(str, keyword[i]) );
}
return 0;
}
int count(char str[], char keyword[])
{
int i, j;
int numofkeyword, numofstr;
int time = 0;
for (numofkeyword = 0; keyword[numofkeyword] != '\0'; numofkeyword++);//计算keyword长度
for (numofstr = 0; str[numofstr] != '\0'; numofstr++);//计算str长度
for (i = 0; str[i] != '\0'; i++)
{
if ( str[i] > 'A' && str[i] < 'Z')
{//全部转换成小写
str[i] = str[i] + 32;
}
}
for (i = 0; keyword[i] != '\0'; i++)
{
if ( keyword[i] > 'A' && keyword[i] < 'Z')
{//全部转换成小写
keyword[i] = keyword[i] + 32;
}
}
for (i = 0; i = 'a' && str[i + j] = 'a' && str[i - 1] = 'a' && str[i - 1] = 'a' && str[i + j]