Count the number of occurrences of each letter in string

前端 未结 15 1991
别跟我提以往
别跟我提以往 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.

提交回复
热议问题