A C program that counts the letters in each word and displays frequency of words with same letters length, tell me where I am wrong

Viewed 51

Please help me find the error in this basic c program. I am a beginner. The program counts the number of letters in each word, then should display the frequency of each letter word being repeated. Thanks in advance.

#include <stdio.h>
int main()
{ int lenCount[20], i, c, state;
  for(i=0; i<=20; i++)
  lenCount[i]=0;
  printf("Enter the words:\n");
  while((c=getchar()!=EOF)) {
    if(c=='\n'||c==' '||c=='\t'){
      lenCount[i] = lenCount[i] +1;
      i = 0;
    }
    else {
      ++i;

    }  
    }
  
  for(i=0; i<=20; i++){
  printf("\n Words with %d letters are repeated %d times", i, lenCount[i]);}
  return 0;
}
1 Answers

Are you wanting the number of characters in each word?
This treats a word as consecutive non-space characters.
The space flag treats consecutive whitespace as a single whitespace.
This does not show the number of words that have the same number of characters. That can be done by sorting the length array.
Have fun.

#include <stdio.h>
#include <ctype.h>

#define SIZE 20

int main ( void) {
    int in = 0;
    int count = 0;
    int index = 0;
    int space = 1; // in case of leading whitespace
    int length[SIZE] = { 0};

    while ( EOF != ( in = getchar ( ))) {
        if ( isspace ( (unsigned char)in)) { // whitespace
            if ( ! space) {
                space = 1; // to treat consecutive whitespace as one whitespace
                length[index] = count; // save the count
                count = 0; // reset the count
                ++index; // next index
                if ( index == SIZE) { // index is at SIZE limit
                    break;
                }
            }
        }
        else { // non-whitespace
            space = 0; // set after getting non-white space
            ++count;
        }
    }

    for ( int each = 0; each < SIZE; ++each) {
        printf ( "word %d had %d characters\n", each + 1, length[each]);
    }

    return 0;
}

Sort and show result by number of characters in each word:

#include <stdio.h>
#include <ctype.h>

#define SIZE 20

void sort ( int *arr, int size) {
    for ( int each = 1; each < size; ++each) {
        for ( int sort = each; sort > 0; --sort) {
            if ( arr[sort] < arr[sort - 1]) {
                int temp = arr[sort];
                arr[sort] = arr[sort - 1];
                arr[sort - 1] = temp;
            }
            else {
                break;
            }
        }
    }
}

int main ( void) {
    int in = 0;
    int count = 0;
    int index = 0;
    int space = 1; // in case of leading whitespace
    int length[SIZE] = { 0};

    while ( EOF != ( in = getchar ( ))) {
        if ( isspace ( (unsigned char)in)) { // whitespace
            if ( ! space) {
                space = 1; // to treat consecutive whitespace as one whitespace
                length[index] = count; // save the count
                count = 0; // reset the count
                ++index; // next index
                if ( index == SIZE) { // index is at SIZE limit
                    break;
                }
            }
        }
        else { // non-whitespace
            space = 0; // set after getting non-white space
            ++count;
        }
    }
    if ( index < SIZE && space == 0) { // due to EOF
        length[index] = count;
    }

    sort ( length, SIZE);

    int match = 1;
    int same = length[0];
    for ( int each = 1; each < SIZE; ++each) {
        if ( same != length[each]) {
            printf ( "%d words had %d characters\n", match, same);
            match = 1;
            same = length[each];
        }
        else {
            ++match;
        }
    }
    printf ( "%d words had %d characters\n", match, same);

    return 0;
}
Related