Count the number of occurrences of each letter in string

前端 未结 15 1953
别跟我提以往
别跟我提以往 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:46
    #include<stdio.h>
    #include<string.h>
    
    #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;
    }
    
    0 讨论(0)
  • 2020-12-10 20:49

    Like this:

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

    This code assumes that the string is null-terminated, and that it contains only characters a through z or A through Z, inclusive.

    To understand how this works, recall that after conversion tolower each letter has a code between a and z, and that the codes are consecutive. As the result, tolower(*p) - 'a' evaluates to a number from 0 to 25, inclusive, representing the letter's sequential number in the alphabet.

    This code combines ++ and *p to shorten the program.

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

    Have checked that many of the answered are with static array, what if suppose I have special character in the string and want a solution with dynamic concept. There can be many other possible solutions, it is one of them.

    here is the solutions with the Linked List.

    #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;
    }
    
    0 讨论(0)
提交回复
热议问题