Count the number of occurrences of each letter in string

前端 未结 15 1951
别跟我提以往
别跟我提以往 2020-12-10 20:02

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 c

相关标签:
15条回答
  • 2020-12-10 20:24

    You can use the following code.

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

    Hope this helps.

    0 讨论(0)
  • 2020-12-10 20:25
    #include <stdio.h>
    #include <string.h>
    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<n;i++)
        {
            ch=tolower(str[i]);
            if(ch>=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;
    }
    
    0 讨论(0)
  • 2020-12-10 20:28
    #include<stdio.h>
    
    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);
    }
    
    0 讨论(0)
  • 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 will store the occurence of any character in the string.

    0 讨论(0)
  • 2020-12-10 20:30

    //This is JavaScript Code.

    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/
    
    0 讨论(0)
  • 2020-12-10 20:33

    Here is the C code with User Defined Function:

    /* C Program to count the frequency of characters in a given String */
    
    #include <stdio.h>
    #include <string.h>
    
    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]++;
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题