0

Finding the length of the last word input in C. At the while loop part, the if statement never reaches the else part, how to fix that? The output for the input "Hello world" should be 5

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

int lsl(char s[]){
    int L;
    char str[5000]={0};
    gets(str);
    
    L = strlen(str);
    int i = L-1;
    while(i>=0){
        if(str[i]!=" ") i--;
        else break;
    }
    printf("%s:\n",str);
    printf("%d:\n",L);
    printf("%d:\n",i);
    printf("%d:\n",L-1-i);
    return L-1-i;
} 

void main(){
    char s[5000];
    lsl(s);
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
Eric Cao
  • 19
  • 3
  • 2
    Don't use `gets`. https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used – William Pursell Aug 03 '21 at 16:15
  • 1
    Unrelated: you the argument `s` to the function if you never use it inside? – pmg Aug 03 '21 at 16:16
  • According to your comments below the answers you compared a pointer with an integer. Your compiler should tell you such things. If not, turn up the warning level. For GCC you can use options `-Wall -Wextra`. If your compiler printed some warning, you should read them, understand them and solve them before you run the program. – Gerhardh Aug 03 '21 at 16:49

2 Answers2

3
 if(str[i]!=" ")

but you mean

if(str[i]!=' ')

Prefer compile with warnings; this compared the ith character in str with the pointer to the constant string consisting of a single space character. With constant string folding this is not utterly meaningless anymore, but it still won't be true. To get single characters instead of strings, use single quotes.

Joshua
  • 40,822
  • 8
  • 72
  • 132
1

For starters according to the C Standard the function main without parameters shall be declared like

int main( void )

Secondly the function lsl

int lsl(char s[]){

does not use its parameter s. The passed character array is not changed within the function.

Thirdly, the function gets is unsafe and is not supported by the C Standard. Instead use the standard C function fgets.

In this if statement

if(str[i]!=" ") i--;

there are compared the character produced by the expression str[i] with the string literal " " that used in the condition is converted to the type char*. That is in this if statement there are compared a character and a pointer that does not make a sense. You need to use the integer character constant ' ' instead of the string literal " ".

if ( str[i] != ' ' ) i--;

But in any case the function is incorrect because the user can enter trailing spaces. In this case you will get an incorrect result.

The function can be declared and defined the following way as it is shown in the demonstrative program below.

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

size_t lsl( char s[], size_t n )
{
    size_t length = 0;
    
    if ( fgets( s, n, stdin ) )
    {
        s[ strcspn( s, "\n" ) ] = '\0';

        size_t i = strlen( s );
        
        while ( i != 0 && isspace( ( unsigned char )s[i -1] ) ) --i;
        
        while ( i != 0 && !isspace( ( unsigned char )s[--i] ) )
        {
            ++length;
        }
    }
    
    return length;
}

int main(void) 
{
    enum { N = 5000 };
    
    char s[N];
    
    size_t length = lsl( s, N );
    
    if ( length != 0 )
    {
        printf( "The length of the last word in the string \"%s\"\n", s );
        printf( "is %zu\n", length );
    }
    
    return 0;
}

If to enter for example the string "Hello World " then the program output will look like

The length of the last word in the string "Hello World   "
is 5
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • One step at a time man. I left the gets method in place because learning is expensive in brain power. (I didn't vote on this answer.) – Joshua Aug 03 '21 at 17:30