0

My program readability.c compiles and gets input. It's supposed to return the number of letters in the input text. I'm getting this error instead of the answer. If you can help me you'll be welcome. Thanks. Here's my code:

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

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


    char n = 1;

    int count_letters = n+1;

    size_t strlen(const char *str);

    {
        if (isalpha(str))
        ;
        printf("%d letter(s)\n", n);
    }

}
1

isalpha() expects a char, not a char *. You could dereference str using str[0], str[1] and so on. You can figure out which line of code is causing the segmentation fault by first clicking to the left of a line of code to set a breakpoint, running your program through debug50, then using the "step over" button at the top of the menu that comes up on the right side of your screen, which is between the pause/play button and the "step into" button (which looks like a down arrow).

Here is the manual page for the group of functions that isalpha() is in: https://man.cs50.io/3/isalpha#description

Here is a thread about debugging: Do YOU know how to find a seg fault?? Advice to new programmers

| improve this answer | |
  • 1
    Thank you so much. Interesting pages, gonna read them all. I'll try what you told me and come back here hopefully having solved the problem. That's what I need: information. – 333PPP Aug 21 at 21:44

You must log in to answer this question.

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