user-0 提问: 求助如何统计字符数组中某个字符串出现的次数

回复:
user: user-1
content: struct strcnt {     char *str;     int   cnt; };  int count(int n, const char **s, struct strcnt *cnt) {     int i, j;     int m;      m = 0;     for (i = 0; i < n; i++) {         for (j = 0; j < m; j++) {             if (0 == strcmp(s[i], cnt[j].str))                 break;         }         if (j < m) {             /* s[i] in cnt */             cnt[j].cnt++;         } else {             /* s[i] not in cnt */             m++;             cnt[j].cnt = 1;             cnt[j].str = strdup(s[i]);         }     }     return m; }  int main(void) {          const char *s[] = {"pasmin123", "pas123456","pasmin@123","pasrftyuiop",                        "pasrftyuiop","as3456",   "pas3456",   "pasmin123",                        "pasmin@123", "pas456",   "pas3456"};     struct strcnt *cnt;     int j;     int m, n;      n = sizeof(s) / sizeof(s[0]);     cnt = calloc(n, sizeof(struct strcnt));     m = count(n, s, cnt);      for (j = 0; j < m; j++) {         printf("%-16s : %4d\n", cnt[j].str, cnt[j].cnt);     }      return 0; }  /* output: pasmin123        :    2 pas123456        :    1 pasmin@123       :    2 pasrftyuiop      :    2 as3456           :    1 pas3456          :    2 pas456           :    1 */

user: user-2
content: #include <stdio.h> #include <stdlib.h> #include <string.h>  struct str_info {     char *str;     int cnt;     struct str_info *next; };   static struct str_info *create_node(const char *str, int len) {     struct str_info *p;      p = (struct str_info *)malloc(sizeof(struct str_info));     if (!p) {         fprintf(stderr, "malloc error!\n");         exit(0);     }     p->str = (char *)malloc(len + 1);     if (!p->str) {         fprintf(stderr, "malloc error!\n");         exit(0);     }     strcpy(p->str, str);     p->next = NULL;      return p; }  void insert_node(struct str_info **pphead, const char *str, int len) {     struct str_info *p = *pphead, *q;      if (*pphead == NULL) {         *pphead = create_node(str, len);         (*pphead)->cnt = 1;         return;     }      q = p;     while (p) {         if (strcmp(p->str, str) == 0) {             p->cnt++;             break;         }         q = p;         p = p->next;     }     if (!p) {         q->next = create_node(str, len);         q->next->cnt = 1;         return;     } }  void print_nodes(struct str_info *phead) {     struct str_info *p;      p = phead;      while (p) {         printf("%s appears %d s!\n", p->str, p->cnt);         p = p->next;     } }  int main(void) {     char str[128];     int len;     struct str_info *phead = NULL;      while (fgets(str, sizeof(str), stdin)) {         len = strlen(str);         if (str[len-1] == '\n')             str[len-1] = 0;         insert_node(&phead, str, len-1);     }      print_nodes(phead);      return 0; }  供参考  链表实现；注意最后输入之后不输入了，用ctrl+d（linux),ctrl+z（windows）结束输入字符串

user: user-3
content: 3#楼的这句有漏洞：        for (int j = 0; j <= len; j ++){应改为：for (int j = 0; j <= len-1; j ++){

user: user-4
content: 俺也仿照整一个纯C的：  #include <stdio.h> #include <string.h> #define N 11 int main() { 	char str[N][15] = { "pasmin123",  "pas123456","pasmin@123","pasrftyuiop", 					  "pasrftyuiop","as3456",   "pas3456",   "pasmin123", 					  "pasmin@123", "pas456",   "pas3456" }; 	int n = 0, i, j; 	struct temp { char* p; int count; } T[N] = { {NULL,0} }; 	for (i = 0; i < N; i++) 	{ 		for (j = 0; j <n; j++)  		{ 			if (strcmp(str[i],T[j].p) == 0)  			{ 				T[j].count ++; 				break; 			} 		} 		if (j == n) 		{ 			T[n].p = str[i]; 			T[n++].count++; 		} 	} 	for (int i = 0; i < n; i++)  		printf("%-15s:%d\n", T[i].p, T[i].count); 	return 0; }

user: user-3
content: 补楼上：//pasmin123      :2 //pas123456      :1 //pasmin@123     :2 //pasrftyuiop    :2 //as3456         :1 //pas3456        :2 //pas456         :1 //请按任意键继续. . .

user: user-3
content: 供参考：#include <stdio.h> #include <string.h>  int main() {     char str[][15] = {"pasmin123",  "pas123456","pasmin@123","pasrftyuiop",                       "pasrftyuiop","as3456",   "pas3456",   "pasmin123",                       "pasmin@123", "pas456",   "pas3456"};      int len = sizeof(str)/sizeof(char[15]);     int *num = new int[len],*flag = new int[len];     memset(num,0,sizeof(int)*len);     memset(flag,0,sizeof(int)*len);      for(int i = 0; i < len; i ++){         for (int j = 0; j <= len; j ++){             if((flag[j]==0)&&(strncmp(str[j], str[i],                 strlen(str[j])>strlen(str[i])?strlen(str[j]):strlen(str[i]))==0)){                 num[i] ++;                 flag[j] = 1;//flag标记已经计数过的字符串             }         }     }     for(int i=0;i<len;i++){        if(num[i] != 0) //重复的只输出一次           printf("%-15s:%d\n",str[i],num[i]);     }     delete []num;     delete []flag;          return 0; }

user: user-5
content: char *str="pasmin123"; long count[128];  char *p=str; while(*p)   {       count[*p]++;       p++; }  a的ASCII码为97 所以统计完成后a的重复数就是count[97]

user: user-4
content: 规模不大就一个一个比较就行， 规模大了就hash一下。  关键词：规模