-2

I am trying to construct a simple programme which counts the number of letters in a user's input. My idea was to use a for loop which would loop for each letter in a string, increment a counter, and return the final counter total at the end. However, I do not want the programme to count spaces- I attempted to use the isalpha function to do this- but I seem to be formatting it incorrectly. Also, the way I tried to make the counter NOT increment for a space was with c = c, however, this also seems to be incorrect. Here is the code I have written so far:

int c = 0;

int main(void)
{
    string s = get_string("Input:  ");
    printf("Output: ");
    for (int i = 0; i < strlen(s); i++)
    {
    if( s[i] (isalpha))
        {
        c++;
        }
    else
        {
        c = c;
        }
    }
    printf("%i\n", c);
}
BlueKhakis
  • 293
  • 1
  • 8
  • 1
    When asking questions, include a [mre]. That means you should include **complete** source code that reproduces the problem. Your code uses `string`, which is not a standard C identifier. Likely, you are working on Harvard’s CS50 course, and your actual text contains a line like `#include "cs50.h"`. You should show this in the source code provided in a question. – Eric Postpischil Dec 05 '20 at 12:19
  • 1
    Never describe a problem as “but I seem to be formatting it incorrectly.” State what error message or undesired behavior you are observing. State what behavior you desire instead. State what you do that results in the undesired behavior. That may be “I compile the source code shown here, and the result is the compiler prints a message “xyz,” but I want the program to compile with no errors.” – Eric Postpischil Dec 05 '20 at 12:21
  • @EricPostpischil Thanks for your advice, I shall make sure to follow these guidelines in future. – BlueKhakis Dec 05 '20 at 12:22

2 Answers2

2

isalpha is effectively a function call. (It is a function call or a function-like macro.) A function call is written as name(), name(argument), or name(argument,…) according to whether it is passing zero, one, or more arguments. isalpha takes a single argument. That argument should be an unsigned char value. So you can call it as isalpha((unsigned char) s[i]). Your if statement should look like:

if (isalpha((unsigned char) s[i]))

Your program should also contain #include <ctype.h> to include the header that declares isalpha.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
  • Thank you very much, do you know what I need to do in order to make the counter not increment for spaces? – BlueKhakis Dec 05 '20 at 12:21
  • 1
    @BlueKhakis: You do not need to do anything in order for the counter not to increment. The braces of the `else` part can be empty, like `else { }`, or you can omit the `else` part entirely. Again, we do not know what problem you had with this because you did not say how it “seems to be incorrect.” We do not know if that means you got an error message from the compiler, or which message, or whether the program did not work the way you want, or whether there was some other problem. – Eric Postpischil Dec 05 '20 at 12:25
  • Okay, thank you, I will be more diligent in describing future questions. – BlueKhakis Dec 05 '20 at 12:28
0

This record

s[i] (isalpha)

is incorrect and does not make a sense.

What you need is the following

size_t c = 0;

for ( size_t i = 0; s[i] != '\0'; i++ )
{
    if( isalpha( ( unsigned char )s[i] ) ) ++c;
}
printf("c = %zu\n", c );

Pay attention to that the function isalpha is declared in the header <ctype.h> that you have to include.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335