0

I'm not asking for the solution, but I'm stuck in getting the integer of total letters. It doesn't compile but it's close, errors say it's something with isalpha. As it doesn't compile, I can't even say whether it works or not. Thank you!

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

int main(void)
{
    string paragraph = get_string("Text: ");

    char n = '\1';

    int count_letters = n++;

    //for(int i = 0; i++;)

    do {
        printf("%i letter(s)\n", n);
    } while(n (isalpha));

}
1

Syntax error. You just need to change it to the correct syntax.

do
{
    ...
} while(isalpha(n));

The isalpha() function takes a parameter (inside the parentheses). That's what will be checked whether it is a letter.

That will fix your compile error with isalpha. But there are still problems with the code.

First, what is this about? char n = '\1'; If you're trying to set n equal to the character 1, then it should be char n = '1'; without the backslash. If you're trying to set it to a value of 1, then you should use char n = 1; It is allowable to use a char variable as a counter, since C treats it like a one byte signed integer as well as a char.

But, if you're really trying to count characters, why not just use count_letters directly?

Finally, the do/while loop is a problem. As written, it will do one of two things. It will either execute exactly once because n is not a letter, or it will run as an infinite loop because n is a letter, and nothing inside the loop will ever change the contents of n.

You should look at doing that for loop again! ;-)

That should get you over some of your issues, and let you keep working, as you requested. Happy programming! ;-)

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

1
  • Thank you so much again! It's better to know it was a syntax problem and my reasoning wasn't so bad after all. Thanks to you it compiled! So I'm giving you the check mark. Thanks! – 333PPP Aug 20 '20 at 21:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .