1

I have a word count program in C that excludes special characters and number digits:

int main(){
    char str[100];
    int i, j = 0;
    int len;
    char choice;
    
    do {
        printf("Enter String: ");
        fgets(str, sizeof(str), stdin);

        len = strlen(str);

        for (i = 0; i < len; i++) {
            if((str[i] >= 'A' && str[i] <= 'Z') || 
              (str[i] >= 'a' && str[i] <= 'z')) //checks if its upper or lowercase {
                if (str[i] != ' ' && (str[i + 1] == ' ' || str[i + 1] == '\0'))//checks if the current character is a space and also the end of the string{
                    j++;
                }
            }
        }

        printf("Word count: %d\n", j);

        printf ("Try again?(Y/N): ");
        scanf (" %c", &choice);    
     
        getchar ();//use to clear input buffer
    } while (choice == 'y' || choice == 'Y');

    return 0;  
}

But it doesn't count the first string after a whitespace, and seems to get a bit messy as I go on the loop.

I tried changing the values of the array, the loop, and also tried to ask AI what I've missed, but the suggestions didn't really work.

output:

Enter String: i
Word count: 0
Try again? (Y/N): Y
Enter String: i have 3 Apples
Word count: 2
Try again? (Y/N): Y
Enter String: hello WORLD
Word count: 3
Try again? (Y/N): hello world
7
  • 3
    Few unrelated comments: Mixing fgets, scanf and getchar is a bad idea. Also use isalpha() and isspace() standard functions instead of your checks. Commented Mar 14, 2024 at 17:36
  • str[i] != ' ' is always true, you can't get there if the character is not a letter. I would suggest stepping through the code in a debugger to see where it is going wrong. Commented Mar 14, 2024 at 17:37
  • 1
    Welcome to Stack Overflow! Please post code, data, and results as text, not screenshots (how to format code in posts). Why should I not upload images of code/data/errors? idownvotedbecau.se/imageofcode Commented Mar 14, 2024 at 17:41
  • 1
    "it doesn't count the first string after a whitespace". Work with a state machine – you are either parsing a word or parsing a space. Begin in the 'space' state and count at the beginning of each word, i.e. when the state changes from 'space' to 'word'. Commented Mar 14, 2024 at 17:49
  • 1
    You also have isalpha() and isspace() etc available to use. Aside: rename the variable j as wordcount. Commented Mar 14, 2024 at 17:52

1 Answer 1

1

For starters the function fgets can append the new line character '\n' to the entered string.

You should either remove it for example like

str[strcspn( str, "\n" )] = '\0';

or take into account in your if statements.

Otherwise as a result you have for example

Enter String: i
Word count: 0

though according to your description the word count should be equal to 1.

Secondly you are not resetting to zero the variable j within the do-while loop.

This expression

str[i] != ' '

in the inner if statement always evaluates to logical true. So its presence in the if statement is redundant.

Also it will be much better to use standard function isalpha instead of comparing characters with symbols. Also the tab character '\t' and also some non-alpha characters can separate words as written in your description of the task but you have ignored that. According to your approach this string "one,two,three" contains one word.

Sign up to request clarification or add additional context in comments.

1 Comment

"much better to use standard function isalpa" --> yes, but then isalpha() needs to get unsigned char values and not char ones, that may be negative.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.