-1

my code counts the number of letters from the given string(excluding white space). cs50.h is a library which consists of get_string() function which takes input from user. the code is compiling but when i give an input which has more than one word, it stops working(doesn't give me anything).can anyone please tell why?(i am using c language)

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

int count_letters(string text);

int main(void)
{
    string input = get_string("Text: ");
    int letters = count_letters(input);
    printf("%i\n",letters);
}

int count_letters(string text)
{
    int i = 0;
    while(text[i] != '\0')
    {
        if(isspace(text[i]))
            continue;

        i++;
    }
    return i;
}
Chris
  • 26,361
  • 5
  • 21
  • 42
  • For a start, take the [tour] and read [ask]. Also, what programming language are you using? [edit] your Q to remove the tag that's not that language. Next time, also make sure you read the description of the tags you apply! – Ulrich Eckhardt Aug 22 '22 at 18:20
  • Show the code for `get_string`. Also, are you using C or C++? – NathanOliver Aug 22 '22 at 18:21
  • 2
    This would have been a great time to learn [how to debug your programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). For example by using a [*debugger*](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) to step through the code statement by statement while monitoring variables and their values. – Some programmer dude Aug 22 '22 at 18:30

2 Answers2

2

You have an infinite while loop when the entered string contains a white space character

int count_letters(string text)
{
    int i = 0;
    while(text[i] != '\0')
    {

        if(isspace(text[i]))
            continue;

        i++;
    }
    return i;
}

because when a white space character is encountered the variable i is not incremented due to the continue statement.

Rewrite the function for example like

int count_letters(string text)
{
    int count = 0;

    for ( ; *text != '\0'; ++text )
    {
        if ( !isspace( ( unsigned char )*text ) )
        {
            ++count;
        }
    }

    return count;
}

Pay attention to that it will be much better if instead of the typedef name string as the parameter type specifier you will use the specifier const char * as for example

int count_letters( const char *text);

because the passed string is not changed within the function.

And instead of the return type int it is better to use the type size_t.

So the function can be declared and defined like

size_t count_letters( const char *text )
{
    size_t count = 0;

    for ( ; *text != '\0'; ++text )
    {
        if ( !isspace( ( unsigned char )*text ) )
        {
            ++count;
        }
    }

    return count;
}

And in main the function is called like

char *input = get_string("Text: ");
size_t letters = count_letters(input);
printf("%zu\n",letters);
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
1

This:

if(isspace(text[i]))
   continue;

means if the character at offset i of text is a space continue.

But continue means go back to the loop condition so i++; won't get executed.

So if your text includes a space your code gets stuck in a non-terminating loop checking the same space character over and over again.

Persixty
  • 8,165
  • 2
  • 13
  • 35