计算字符串中每个字母的出现次数

如何计算字符串中每个字母(忽略大小写)中c的出现次数? 因此它会打印出来的letter: # number of occurences ,我有代码来计算一个字母的出现次数,但我如何计算字符串中每个字母的出现次数?

 { char int count = 0; int i; //int length = strlen(string); for (i = 0; i < 20; i++) { if (string[i] == ch) { count++; } } return count; } 

输出:

 a : 1 b : 0 c : 2 etc... 

假设您有一个char为8位的系统,并且您尝试计算的所有字符都使用非负数进行编码。 在这种情况下,您可以写:

 const char *str = "The quick brown fox jumped over the lazy dog."; int counts[256] = { 0 }; int i; size_t len = strlen(str); for (i = 0; i < len; i++) { counts[(int)(str[i])]++; } for (i = 0; i < 256; i++) { printf("The %d. character has %d occurrences.\n", i, counts[i]); } 

请注意,这将计算字符串中的所有字符。 如果你100%绝对肯定你的字符串里面只有字母(没有数字,没有空格,没有标点符号),那么1.要求“不区分大小写”开始有意义,2。你可以减少条目数到英文字母中的字符数(即26),你可以这样写:

 #include  #include  #include  const char *str = "TheQuickBrownFoxJumpedOverTheLazyDog"; int counts[26] = { 0 }; int i; size_t len = strlen(str); for (i = 0; i < len; i++) { // Just in order that we don't shout ourselves in the foot char c = str[i]; if (!isalpha(c)) continue; counts[(int)(tolower(c) - 'a')]++; } for (i = 0; i < 26; i++) { printf("'%c' has %2d occurrences.\n", i + 'a', counts[i]); } 

像这样:

 int counts[26]; memset(counts, 0, sizeof(counts)); char *p = string; while (*p) { counts[tolower(*p++) - 'a']++; } 

此代码假定字符串以空值终止,并且它仅包含字符azAZ (包括)。

要理解这是如何工作的,请回想一下,在转换后,每个字母都有一个代码在az之间,并且代码是连续的。 结果, tolower(*p) - 'a'计算结果为025的数字,包括字母表中字母的连续数字。

此代码结合++*p来缩短程序。

一个简单的可能性是制作一个26个整数的数组,每个都是字母az的计数:

 int alphacount[26] = {0}; //[0] = 'a', [1] = 'b', etc 

然后遍历字符串并增加每个字母的计数:

 for(int i = 0; i 

这是一个适用于AZ,az的简单想法。 如果你想用大写字母分开,你只需要改变计数52并减去正确的ASCII偏移量

 #include  #include  void main() { printf("PLEASE ENTER A STRING\n"); printf("GIVE ONLY ONE SPACE BETWEEN WORDS\n"); printf("PRESS ENETR WHEN FINISHED\n"); char str[100]; int arr[26]={0}; char ch; int i; gets(str); int n=strlen(str); for(i=0;i=97 && ch<=122) { arr[ch-97]++; } } for(i=97;i<=122;i++) printf("%c OCCURS %d NUMBER OF TIMES\n",i,arr[i-97]); return 0; } 

接受答复后

符合这些规格的方法:( IMO,其他答案不符合所有要求)

  1. char具有广泛的范围时,它是实用/有效的。 示例: CHAR_BIT1632 ,因此不使用bool Used[1 << CHAR_BIT];

  2. 适用于非常长的字符串(使用size_t而不是int )。

  3. 不依赖于ASCII。 ( Use Upper[]

  4. char <0时定义的行为is...()函数是为EOFunsigned char定义的

     static const char Upper[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; static const char Lower[] = "abcdefghijklmnopqrstuvwxyz"; void LetterOccurrences(size_t *Count, const char *s) { memset(Count, 0, sizeof *Count * 26); while (*s) { unsigned char ch = *s; if (isalpha(ch)) { const char *caseset = Upper; char *p = strchr(caseset, ch); if (p == NULL) { caseset = Lower; p = strchr(caseset, ch); } if (p != NULL) { Count[p - caseset]++; } } } } // sample usage char *s = foo(); size_t Count[26]; LetterOccurrences(Count, s); for (int i=0; i<26; i++) printf("%c : %zu\n", Upper[i], Count[i]); } 

您可以使用以下代码。

 main() { int i = 0,j=0,count[26]={0}; char ch = 97; char string[100]="Hello how are you buddy ?"; for (i = 0; i < 100; i++) { for(j=0;j<26;j++) { if (tolower(string[i]) == (ch+j)) { count[j]++; } } } for(j=0;j<26;j++) { printf("\n%c -> %d",97+j,count[j]); } } 

希望这可以帮助。

 #include #include #define filename "somefile.txt" int main() { FILE *fp; int count[26] = {0}, i, c; char ch; char alpha[27] = "abcdefghijklmnopqrstuwxyz"; fp = fopen(filename,"r"); if(fp == NULL) printf("file not found\n"); while( (ch = fgetc(fp)) != EOF) { c = 0; while(alpha[c] != '\0') { if(alpha[c] == ch) { count[c]++; } c++; } } for(i = 0; i<26;i++) { printf("character %c occured %d number of times\n",alpha[i], count[i]); } return 0; } 
 for (int i=0;i 
 #include void frequency_counter(char* str) { int count[256] = {0}; //partial initialization int i; for(i=0;str[i];i++) count[str[i]]++; for(i=0;str[i];i++) { if(count[str[i]]) { printf("%c %d \n",str[i],count[str[i]]); count[str[i]]=0; } } } void main() { char str[] = "The quick brown fox jumped over the lazy dog."; frequency_counter(str); } 

这是带有用户定义函数的C代码:

 /* C Program to count the frequency of characters in a given String */ #include  #include  const char letters[] = "abcdefghijklmnopqrstuvwxzy"; void find_frequency(const char *string, int *count); int main() { char string[100]; int count[26] = { 0 }; int i; printf("Input a string: "); if (!fgets(string, sizeof string, stdin)) return 1; find_frequency(string, count); printf("Character Counts\n"); for (i = 0; i < 26; i++) { printf("%c\t%d\n", letters[i], count[i]); } return 0; } void find_frequency(const char *string, int *count) { int i; for (i = 0; string[i] != '\0'; i++) { p = strchr(letters, string[i]); if (p != NULL) { count[p - letters]++; } } } 
 int charset[256] = {0}; int charcount[256] = {0}; for (i = 0; i < 20; i++) { for(int c = 0; c < 256; c++) { if(string[i] == charset[c]) { charcount[c]++; } } } 

charcount将存储字符串中任何字符的出现。

//这是JavaScript代码。

 function countWordOccurences() { // You can use array of words or a sentence split with space. var sentence = "The quick brown fox jumped over the lazy dog."; //var sentenceArray = ['asdf', 'asdf', 'sfd', 'qwr', 'qwr']; var sentenceArray = sentence.split(' ', 1000); var output; var temp; for(var i = 0; i < sentenceArray.length; i++) { var k = 1; for(var j = i + 1; j < sentenceArray.length; j++) { if(sentenceArray[i] == sentenceArray[j]) k = k + 1; } if(k > 1) { i = i + 1; output = output + ',' + k + ',' + k; } else output = output + ',' + k; } alert(sentenceArray + '\n' + output.slice(10).split(',', 500)); } You can see it live --> http://jsfiddle.net/rammipr/ahq8nxpf/ 

// c代码用于计算字符串中每个字符的出现次数。

 void main() { int i,j; int c[26],count=0; char a[]="shahid"; clrscr(); for(i=0;i<26;i++) { count=0; for(j=0;j 
 protected void btnSave_Click(object sender, EventArgs e) { var FullName = "stackoverflow" char[] charArray = FullName.ToLower().ToCharArray(); Dictionary counter = new Dictionary(); int tempVar = 0; foreach (var item in charArray) { if (counter.TryGetValue(item, out tempVar)) { counter[item] += 1; } else { counter.Add(item, 1); } } //var numberofchars = ""; foreach (KeyValuePair item in counter) { if (counter.Count > 0) { //Label1.Text=split(item. } Response.Write(item.Value + " " + item.Key + "
"); // Label1.Text=item.Value + " " + item.Key + "
"; spnDisplay.InnerText= item.Value + " " + item.Key + "
"; } }