-1
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define N 100

int main()
{

    int count[26] = {0};
    int i;
    char ch ='a';

    printf("Enter a sentence (end by '.'): ");

    while (ch != '.') {
        ch = getchar();

        count[(tolower(ch) - 'a')]++; 

        for (i = 0; i < 26; i++) {
            printf("'%c' has %2d occurrences.\n", i + 'a', count[i]); 
        }
    }       

    return 0;
}

The program does what it is supposed to do. The counter works fine the issue is that the program runs though every single letter twice and prints out which letters occurs 0 times. The correct output should be as follows:

Enter a sentence end by '.'

The scanf reads This.

Correct Output:

T occurs 1 times

H occurs 1 times

I occurs 1 times

S occurs 1 times

But the output from the code goes through every single letter and also prints out which letters did not occur at all. I need to get rid of the letters that occur "0 times" and only display letters that occur 1 or more times.

Any help would be appreciated!

Steve
  • 1,747
  • 10
  • 18
  • wrap your `printf` in a conditional, `if (count[i] > 0){ printf(...); }` – yano Oct 16 '17 at 20:08
  • 1
    Well yes, you loop over all the letter (indices), and in the loop you unconditionally `printf` the number of appearances of the corresponding letter. If you want to skip letters with zero appearances then test the number of appearances before you print. – John Bollinger Oct 16 '17 at 20:09
  • Found this [link](https://stackoverflow.com/questions/3867890/count-character-occurrences-in-a-string) helpful – Omid N Oct 16 '17 at 20:19

4 Answers4

0

Just needs a simple if to check if the count is more than 0:

for (i = 0; i < 26; i++) {
    if (count[i] > 0)
    {
        printf("'%c' has %2d occurrences.\n", i + 'a', count[i]); 
    }
}
Steve
  • 1,747
  • 10
  • 18
  • The edit only displays the first letter with amount of occurrences. –  Oct 16 '17 at 20:10
0

The following worked great for me:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define N 100

int main()
{

    int count[26] = {0};
    int i;
    char ch ='a';

    printf("Enter a sentence (end by '.'): ");

    while (ch != '.') {
        ch = getchar();

        count[(tolower(ch) - 'a')]++;

        for (i = 0; i < 26; i++) {
            if (count[i] > 0) {
                printf("'%c' has %2d occurrences.\n", i + 'a', count[i]);
            }
        }
    }

    return 0;
}
Dror Moyal
  • 398
  • 3
  • 11
0

I suggest you read the input into a character array, say input with fgets() and then loop through the individual characters.

Note that fgets() will read in the trailing \n (newline) which you may want to remove.

char input[100];
fgets(input, sizeof(input), stdin);

You may check the return value of fgets() to see if it succeeded. It returns NULL upon error.

Note that fgets() will read in the trailing newline (\n) into input as well.

You can remove it like

input[strlen(input)-1]='\0';

Now look at each character in input.

for(i=0; input[i]!='.' && input[i]!='\0'; ++i)
{
    count[tolower(input[i])-'a']++;
}

You can make sure that input has a . in it with

if(strchr(input, '.')!=NULL)

strchr() would return NULL if the character is not present in the string.

Finally print the results as you did.

for(i=0; i<26; i++)
{
    if(count[i]>0)
    {
        printf("\n%c has %2d.", i+'a', count[i]);
    }
}
J...S
  • 5,079
  • 1
  • 20
  • 35
0
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define N 100

int main()

{

int count[26] = {0};
int i;
char ch ='a';

printf("Enter a sentence (end by '.'): ");

while (ch != '.') {
    ch = getchar();

    count[(tolower(ch) - 'a')]++;

    for (i = 0; i < 26; i++) {
        if (count[i] > 0) {
            printf("'%c' has %2d occurrences.\n", i + 'a', count[i]);
        }
    }
}

return 0;

}

chrisHG
  • 80
  • 1
  • 2
  • 18