编写函数 void count(char a[],char w[][10],int n,int b[]).功能是:统计w指向的数组中的n个单词在a指向的字符串中各自出现的次数(将非字符字符看作单词分割

来源:互联网 发布:大数据职业规划 编辑:程序博客网 时间:2024/06/05 00:16
/*编写函数 void count(char a[],char w[][10],int n,int b[]).功能是:统计w指向的数组中的n个单词在a指向的字符串中各自出现的次数(将非字符字符看作单词分割符),将统计结果依次保存在b指向的数组中*/#include <stdio.h>#include <string.h>#include <stdlib.h>#define N 10void count(char a[],char w[][10],int n,int b[]){int count = 0;                                          //统计单词出现的次数int count1 = 0;                                         //统计b数组的长度int i = 0;int j = 0;char *ptr = a;for(j = 0;j < n;j++){while(*ptr){if(strncmp(ptr,w[j],strlen(w[j])) == 0){count++;ptr += strlen(w[j]);}else{ptr++;}}b[count1] = count;count1++;count = 0;ptr = ptr-strlen(a);}for(i = 0;i < count1;i++){printf("%d ",b[i]);}printf("\n");}int main(){char a[100] = {0};char w[10][10] = {0};int b[N] = {0};                                         //存放统计结果int n = 0;                                              //统计单词的个数int i = 0;printf("please input string a:\n");scanf("%s",a);printf("please input n:\n");scanf("%d",&n);printf("please input string w:\n");for(i = 0;i < n;i++){scanf("%s",w[i]);}count(a,w,n,b);return 0;}

0 0
原创粉丝点击