1

I want to get the number of letters of the longest word. If I input something like "hello me" I get 5 but if I write something longer like "league of legends" I get 6 instead of 7. Why?

#include <stdio.h>
int longest_word(const char string[]){
    int i;
    int max;
    int cont;
    i=0;
    while(string[i]!='\0'){
        for(cont=0;string[i]!=' '&& string[i]!='\0';i++)
            cont++;
        if (cont>max)
            max=cont;
        ++i;
    }
    return max;
}
int main(void){
    char f[100];    #maybe this is the problem?
    int i;
    printf("input a string: ");
    scanf("%s",f);
    i=longest_word(f);
    printf("%d",i);
    return 0;
}
Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179
Bluuu
  • 75
  • 1
  • 6
  • 2
    Don't use `#define and &&` because it makes your code impossible to read. Also, translate your variable names into English because it makes it easier for us to help you, because we'll know what you're trying to accomplish. – Leo Izen Jun 18 '14 at 01:19
  • For books, see [The Definitive C Book Guide and List](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list). – Jonathan Leffler Jun 18 '14 at 01:22
  • 1
    As for a book on C? I recommend "The C Programming Language" by Brian W. Kernighan and Dennis Ritchie, 2nd edition, ANSI C. (Not 1st edition). – Leo Izen Jun 18 '14 at 01:22
  • Note that `` from AMD1:1994 provides a series of definitions that include the equivalent of `#define and &&`. And in C++98, `and` is a full keyword. – Jonathan Leffler Jun 18 '14 at 01:23
  • 1
    The problem is in your `for` loop. Run it under debugger and you will see. – PM 77-1 Jun 18 '14 at 01:24
  • `RAII` Make sure you initialize variables to default values, this is good practice. `int max = 0;` – Montaldo Jun 18 '14 at 08:22

4 Answers4

5

One of the simplest ways of debugging is to print the data you get to make sure the program got what you think it got.

With scanf(), the %s format reads a single 'word', stopping at the first white space. If you printed f immediately after the call to scanf():

printf("Input: <<%s>>\n", f);

you would see that it contains just 'league' so it gives 6 correctly. Strictly, you should check that scanf() actually got some input before using it:

if (scanf("%99s", f) != 1)
    …EOF or error…

You would either need to use fgets() to read a whole line, or call scanf() and longest_word() iteratively to get as far as 'legends' and the answer 7. Note that your code would count a newline (such as is kept at the end of a line by fgets()) as part of a word. You might want to check the <ctype.h> header and use the isspace() macro to test for white space.

Also, as first pointed out by pablo1977 in his answer, you need to initialize max in longest_word().

Community
  • 1
  • 1
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • Could you please explain me what does this:"printf("Input: <<%s>>\n", f);" exactly do? – Bluuu Jun 18 '14 at 01:46
  • It would display a line `Input: <>`. The angle brackets simply clearly delimit where the string ends. It helps if you find funny situations such as if the string in `f` contains a `\r` character, because then you might see `>>put: <>`; maybe you'd prefer `()` or `[]` — they all work and help make it easier to see what the program got. (Also, did you consider trying it out?) – Jonathan Leffler Jun 18 '14 at 01:48
  • Thanks for the help! I think i got it now :) – Bluuu Jun 18 '14 at 02:22
  • Keep an eye out for [BLUEPIXY](http://stackoverflow.com/users/971127/bluepixy)'s [fix](http://stackoverflow.com/a/24275919/15168) to your loop end condition. Your code is OK if you have multiple null bytes at the end; very often, though, you will only have a single null terminator byte and your code could go haywire. – Jonathan Leffler Jun 18 '14 at 02:25
2
#include <stdio.h>

int longest_word(const char string[]){
    int i;
    int max=0;//need initialize
    int cont;
    i=0;
    while(string[i]!='\0'){
        for(cont=0;string[i]!=' '&& string[i]!='\0';i++)
            cont++;
        if (cont>max)
            max=cont;
        if(string[i]==' ')//Do not increment when string[i]=='\0',,
           ++i;
    }
    return max;
}
int main(void){
    char f[100];
    int i;
    printf("input a string: ");
    //scanf("%s",f);// a string delimited by white spaces
    scanf("%99[^\n]",f);
    i=longest_word(f);
    printf("%d\n",i);
    return 0;
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70
0

The variable max needs to be initialized.

I think this is the problem.

pablo1977
  • 4,281
  • 1
  • 15
  • 41
0

Use getc() in a loop instead of scanf (), as scanf will not consider the input after ' ','\n'...,

Add printf() after scanf() statement., U will come to know.

  • No, what happens is that `scanf(3)` does skip over spaces (that includes `\t` and `\n`). Please check the relevant documentation before answering. – vonbrand Jun 18 '14 at 08:01