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

4

我该如何统计字符串中每个字母(不区分大小写)在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...
15个回答

0

已经检查过很多答案都是使用静态数组,但如果字符串中有特殊字符并且想要使用动态概念的解决方案怎么办。可能还有许多其他可能的解决方案,这只是其中之一。

以下是使用链表的解决方案。

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

struct Node { 
    char data; 
    int counter;
    struct Node* next; 
};

void printLinkList(struct Node* head)
{
    while (head != NULL) { 
        printf("\n%c occur %d", head->data, head->counter);
        head = head->next;
    }
}

int main(void) {
    char *str = "!count all the occurances of character in string!";
    int i = 0;
    char tempChar;
    struct Node* head = NULL; 
    struct Node* node = NULL; 
    struct Node* first = NULL; 

    for(i = 0; i < strlen(str); i++)
    {
        tempChar = str[i];

        head = first;

        if(head == NULL)
        {
            node = (struct Node*)malloc(sizeof(struct Node));
            node->data = tempChar;
            node->counter = 1;
            node->next = NULL;

            if(first == NULL)
            {
                first = node;
            }
        }
        else
        {
            while (head->next != NULL) { 
                if(head->data == tempChar)
                {
                    head->counter = head->counter + 1;
                    break;
                }
                head = head->next;
            }

            if(head->next == NULL)
            {
                if(head->data == tempChar)
                {
                    head->counter = head->counter + 1;
                }
                else
                {
                    node = (struct Node*)malloc(sizeof(struct Node));
                    node->data = tempChar;
                    node->counter = 1;
                    node->next = NULL;
                    head->next = node;
                }
            }
        }
    }

    printLinkList(first);


    return 0;
}

-1

//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<strlen(a);j++)
                {
                 if(a[j]==97+i)
                    {
                     count++;
                         }
                           }
                  c[i]=count;
               }
              for(i=0;i<26;i++)
               {
              j=97+i;
          if(c[i]!=0) {  printf("%c of %d times\n",j,c[i]);
                 }
              }
           getch();
           }

这里有很多问题:这个解决方案效率极低,代码缩进不规则,它使用了硬编码的 ASCII 值、非标准函数,main() 的原型无效,头文件丢失。 - chqrlie

-1
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将存储字符串中任何字符的出现次数。


这段代码有问题:charset 的所有元素都是 0 - chqrlie

-1

//这是 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/

这不是问题的答案... OP要求的是C程序,而不是JavaScript。 - Peter Brittain

-1
protected void btnSave_Click(object sender, EventArgs e)
    {           
        var FullName = "stackoverflow"

        char[] charArray = FullName.ToLower().ToCharArray();
        Dictionary<char, int> counter = new Dictionary<char, int>();
        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<char, int> item in counter)
        {
            if (counter.Count > 0)
            {
                //Label1.Text=split(item.
            }
            Response.Write(item.Value + " " + item.Key + "<br />");
            // Label1.Text=item.Value + " " + item.Key + "<br />";
            spnDisplay.InnerText= item.Value + " " + item.Key + "<br />";
        }

    }

1
为当前的问题提供一些解释,说明该答案如何帮助提问者解决问题。 - ρяσѕρєя K

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接