1

What I'm doing wrong here in this code? Take input as a one string.than Print the length of each word. for example, the length of i is 1 and length of Love is 4 so print the length after the each word.

include

include

int main()
{
    int i,n,count=0;
    char str[20];
    gets(str);
    n=strlen(str);
    for(i=0;i<n;i++){
        if(str[i]==" "){
            printf("%d",count);
            count=0;
        }else{
            printf("%c",str[i]);
            count++;
        }
    }

    return 0;
}
KitKAt
  • 31
  • 6
  • What output are you getting? – Chris Turner Mar 01 '19 at 12:15
  • 1
    don't use the gets it s too dangerous use fgets instead – Spinkoo Mar 01 '19 at 12:18
  • 1
    `if(str[i]==" ")` should be `if(str[i] == ' ')`. In the former case, you are comparing a character and a string literal. The second one compares two characters which is what you want. Compiling with warnings enabled (`-Wall -Wextra`) would've been helpful in spotting the error. Also, as mentioned in the above comment, never use `gets`. – Spikatrix Mar 01 '19 at 12:22
  • @ChrisTurner I am getting output same as a input. – KitKAt Mar 01 '19 at 12:33
  • [Why is the gets function so dangerous that it should not be used?](https://stackoverflow.com/q/1694036/995714) – phuclv Mar 01 '19 at 12:35

2 Answers2

1

The line if(str[i]==" "){ is wrong.
" " is a string and it consists of two bytes: the space character and a terminating NUL character.
You should use if(str[i]==' '){ instead.
' ' is a character and you should compare it with str[i], which is also a character.

Also, it seems you forgot to print a space character after the numbers.

One more point is that you should print the length of the last word even if there isn't a space character after the last word.

By the way, you shouldn't use gets(), which has unavoidable risk of buffer overrun, deprecated in C99 and deleted from C11. You should use fgets(), which takes buffer size, instead. fgets() saves newline characters read to the buffer while gets() doesn't, so you should remove the newline characters if you don't want them.

An example of corrected code:

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

int main()
{
    int i,n,count=0;
    char str[20 + 1]; // allocate one more character for a newline character
    char* lf; // for searching for a newline character
    fgets(str, sizeof(str), stdin); // use fgets() instead if gets()
    if ((lf = strchr(str, '\n')) != NULL) *lf = '\0'; // remove a newline character if one exists
    n=strlen(str);
    for(i=0;i<=n;i++){ // change < to <= for processing the terminating NUL character
        if(str[i]==' ' || str[i]=='\0'){ // compare with a character, not a string
            if (count>0) printf("%d",count); // avoid duplicate printing for duplicate space characters
            if(i+1<n) printf(" "); // print a space if the string continues
            count=0;
        }else{
            printf("%c",str[i]);
            count++;
        }
    }

    return 0;
}
MikeCAT
  • 73,922
  • 11
  • 45
  • 70
0

A few issues in the code:

  • Comparing a character with a string literal.

  • No space after printing a particular word and its length.

  • For the last word, the comparison has to be with a null-terminator.

  • Not taking the null-terminator into account in the condition check of the for loop.

Making these changes, you will get the required output.

Demo here.

Note: gets is deprecated and considered dangerous to use. Use fgets instead.

P.W
  • 26,289
  • 6
  • 39
  • 76