Why won't my code print the required numbers of text?

Andrewoca :

I am trying to create a function as below that would count the number of letters in an inputted text and spit out an integer value. My code below compiles but it won't print out the result. Am I missing something?

// Libraries
#include <cs50.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <ctype.h>

int count_letter(string text)
{
    int lettercount;
    int number_of_letters;

    number_of_letters = strlen(text);

    for(lettercount = 0; lettercount < number_of_letters;)
        if (isalpha(number_of_letters))
            lettercount++;


    return lettercount;
}


int main(void)
{

    string text = get_string("text: ");


    {
        printf("%i letter(s)", count_letter(text));
        printf("\n");
    }



}
DinoCoderSaurus :

Since number_of_letters is an int, what do you think isalpha(number_of_letters) evaluates to? Not to mention there are no braces {} surrounding the for loop or the if in the function which makes the code hard to read, and could in fact cause results you don't expect.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=367731&siteId=1