算上串每个字母出现的次数 [英] Count the number of occurrences of each letter in string

查看:164
本文介绍了算上串每个字母出现的次数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎么能算在字符串中的每个字母(不区分大小写)的Ç出现的次数?因此,它会打印出信:发生的#号,我有code算一个字母的OCCURENCES,但我怎么能算每个字母的发生在字符串?

  {
    烧焦
    诠释计数= 0;
    INT I;    // INT长度= strlen的(字符串);    对于(i = 0; I< 20;我++)
    {
        在(string [I] == CH)
        {
            算上++;
        }
    }    返回计数;
}

输出:

  A:1
乙:0
C:2
等等...


解决方案

让我们假设你有一个系统,其中字符是八位,所有你想要的字符数均设有连接使用非负数codeD。在这种情况下,你可以写:

 为const char *海峡=敏捷的棕色狐狸跳过了懒狗INT数[256] = {0};INT I;
size_t型LEN = strlen的(STR);对于(i = 0; I< LEN,我++){
    计数[(INT)(STR [I])] ++;
}对于(i = 0; I< 256;我++){
    的printf(%d个字符有%d次\\ n。我,计数[I]);
}

请注意,这将计算字符串中的所有字符。如果你是100%绝对正确保您的字符串将只有字母(没有数字,没有空格,没有标点符号)内,然后按1请求的情况下不敏感变得有意义,二可以减少条目的数量在英文字母(即26)中的字符数,你可以这样写:

 的#include<&文件ctype.h GT;
#包括LT&;&string.h中GT;
#包括LT&;&stdlib.h中GT;为const char *海峡=TheQuickBrownFoxJumpedOverTheLazyDog;INT计数[26] = {0};INT I;
size_t型LEN = strlen的(STR);对于(i = 0; I< LEN,我++){
    //只是为了我们不喊自己的脚
    焦C = str中[I]
    如果(因而isalpha(三)!)继续;    计数[(INT)(tolower的(三) - '一')] ++;
}对于(i = 0; I< 26;我++){
    的printf(%C'有%2D出现\\ n,我+'A',计数[I]);
}

How can I count the number of occurrences in c of each letter (ignoring case) in the string? So that it would print out letter: # number of occurences, I have code to count the occurences of one letter, but how can I count the occurence of each letter in the string?

{
    char
    int count = 0;
    int i;

    //int length = strlen(string);

    for (i = 0; i < 20; i++)
    {
        if (string[i] == ch)
        {
            count++;
        }
    }

    return count;
}

output:

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

解决方案

Let's assume you have a system where char is eight bit and all the characters you're trying to count are encoded using a non-negative number. In this case, you can write:

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]);
}

Note that this will count all the characters in the string. If you are 100% absolutely positively sure that your string will have only letters (no numbers, no whitespace, no punctuation) inside, then 1. asking for "case insensitiveness" starts to make sense, 2. you can reduce the number of entries to the number of characters in the English alphabet (namely 26) and you can write something like this:

#include <ctype.h>
#include <string.h>
#include <stdlib.h>

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]);
}

这篇关于算上串每个字母出现的次数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆