计算一个字符串中每个字符出现的次数
2010-01-21 16:42
651 查看
/************************************************************************ ***By : summon *** ***Date : 2010.01.21 *** ***Function : 求一个字符串中每个字符出现的次数 *** ***Method1 : 把这个字符串用快速排序处理,处理完后相同的字符全部临接 *** ***Method2 : 用空间换时间,首先声明一个静态数组count[128],每个字符 *** *** ch出现一次就把count[ch]++。 *** /************************************************************************/ #include <stdlib.h> #include <stdio.h> #include <string.h> #include <iostream.h> //////////////////////////Method1//////////////////////////////////////// #define MAXLEN 128 typedef int STATUS; typedef struct _Result { char ch; unsigned int times; }Result; typedef struct _Node { Result res; _Node *next; }Node; typedef struct _Linker { Node *head; int length; }Linker; STATUS InputString(char *&InputData); STATUS QSortString(char *InputData); STATUS Method1(char *InputData, Linker *&lin); STATUS OutputResult(Linker *lin); int cmpCHAR(const void *a, const void *b); //////////////////////////////////////////////////////////////////////// //////////////////////////Method2/////////////////////////////////////// static unsigned int count[128]; STATUS InputString2(char *&InputData); STATUS Method2(char *InputData); STATUS OutputResult2(); //////////////////////////////////////////////////////////////////////// int main(int argc, char* argv[]) { int ret = -1; char *InputData; Linker *lin; ret = InputString(InputData); ret = QSortString(InputData); ret = Method1(InputData, lin); ret = OutputResult(lin); ret = InputString2(InputData); ret = Method2(InputData); ret = OutputResult2(); return 0; } int cmpCHAR(const void *a, const void *b) { return *(char *)a - *(char *)b; } STATUS InputString(char *&InputData) { char temp[100]; memset(temp, 0, 100); printf("Please input a string!/n"); fflush(stdin); cin.getline(temp, 100); int len = strlen(temp); InputData = (char *)malloc(sizeof(char)*(len + 1)); memcpy(InputData, temp, len); InputData[len] = '/0'; return 1; } STATUS QSortString(char *InputData) { if (InputData == NULL) { return 0; } int len = strlen(InputData); qsort(InputData, len, sizeof(char), cmpCHAR); return 1; } STATUS Method1(char *InputData, Linker *&lin) { if (InputData == NULL) { return 0; } lin = (Linker *)malloc(sizeof(Linker)); lin->head = NULL; lin->length = 0; Node *temp = (Node *)malloc(sizeof(Node)); lin->head = temp; lin->head->res.ch = InputData[0]; lin->head->res.times = 1; lin->head->next = NULL; lin->length += 1; int len = strlen(InputData); for (int i=1; i<len; i++) { if (InputData[i-1] == InputData[i]) { temp->res.times += 1; }else { Node *temp2 = (Node *)malloc(sizeof(Node)); temp->next = temp2; temp2->res.ch = InputData[i]; temp2->res.times = 1; temp2->next = NULL; temp = temp2; } } return 1; } STATUS OutputResult(Linker *lin) { if (lin == NULL) { return 0; } Node *temp = lin->head; while (temp->next != NULL) { printf("Charector :%c, Times: %d/n", temp->res.ch, temp->res.times); temp = temp->next; } printf("Charector :%c, Times: %d/n", temp->res.ch, temp->res.times); return 1; } STATUS InputString2(char *&InputData) { return InputString(InputData); } STATUS Method2(char *InputData) { if (InputData == NULL) { return 0; } for (int j=0; j<128; j++) { count[j] = 0; } int len = strlen(InputData); for (int i=0; i<len; i++) { count[InputData[i]]++; } return 1; } STATUS OutputResult2() { for (int i=0; i<128; i++) { if (count[i] != 0) { printf("Charector: %c, Times: %d/n", i, count[i]); }else { continue; } } return 1; }
相关文章推荐
- php如何计算一个字符串的每个字符出现的次数
- 用es6的Array.reduce()方法计算一个字符串中每个字符出现的次数
- C# 计算字符串中每个字符出现的次数
- 手动输入一个字符串,仅限小写字母,统计并输出每个字符在字符串中出现的次数,并输出。
- 计算给定字符串中每个字符出现的次数,并以指定格式输出,例如aabbbc输出a2b3c1(java语言)
- 小方法1——CountChar--统计一个字符串中每个字符出现的次数
- JavaScript计算字符串中每个字符出现次数的小例子
- Java给定一个字符串数组,判断每个字符出现次数
- 统计一个字符串中每个字符出现的次数
- 在控制台输入一个字符串 判断每个字符出现的次数
- 从文件读取包含数字和字母字符串,统计每个字符出现的次数,将次数输出到另外一个文件
- 计算出一字符串(字符串中每个单词之间有一个或多个空格)中每个单词的 出现的次数
- JavaScript计算字符串中每个字符出现次数的小例子
- JavaScript 计算一个字符串中出现次数最多的字符
- 统计一个字符串中每个字符出现的次数
- 求一个字符串中每个字符出现的次数
- 计算字符串中每个字符出现的次数
- Python—求一个字符串中每个字符出现的次数
- JavaScript计算一个字符串最多重复的字符及出现次数
- 编程题--给定一个字符串数组,判断每个字符出现次数