0
#include<stdio.h>
#include<string.h>
int main(){
      char sen[100];
      int i=0,len;
      printf("Enter a senetence\n");
      scanf("%s",sen);
      len=strlen(sen);
      char ch=sen[i];
      while(i<len){
        if(ch==' '){
            printf("\n");
            i++;
            ch=sen[i];
            continue;
        } //i dont know what is the flaw with the logic.
        printf("%c",ch);
        i++;
        ch=sen[i];
        
      }
      return 0;
}

The aim of the program is to print the words of a sentence separately. However, this program is successful in printing only the first word. Please tell me where I am going wrong.

ASR
  • 9
  • 1
  • 2
  • Add `printf("sen = \"%s\"\n", sen);` right after the scanf and see what happens. – Jabberwocky Dec 09 '21 at 13:07
  • 2
    Does this answer your question? [Scanf reads only one word from sentence](https://stackoverflow.com/questions/65892940/scanf-reads-only-one-word-from-sentence) –  Dec 09 '21 at 13:08

1 Answers1

1

You get just one word as input. You can scanf the input like:

scanf("%[^\n]s",sen);
Deepak
  • 2,660
  • 2
  • 8
  • 23