0

input:--
3
hi hello
my name is abc
this is a test

output:--
hi hello
my name is abc
this is a test

here is the code:--

#include<stdio.h>
main()
{
      int n,i;
      char b[100],*c;
      scanf("%d",&n);
      for(i=0;i<n;i++)
      {
                      gets(b);
                      c=b;
                      while(*c !='\0')
                      {
                               putchar(*c);
                               c++;
                      }
                      printf("\n");
      }
       return 0;
}

My code is not getting right answer for n=1.
In the code I cant use scanf() coz i have to take space also as an input.
and if I am using gets() the for loop first for loop for n continues without taking any input and print next line, I am not understanding why this is happening and how to correct..please guide me..

2
Contributors
1
Reply
4
Views
6 Years
Discussion Span
Last Post by Narue
1

Without getting into the serious issues of your code, the problem is scanf leaving a newline in the stream. gets terminates on a newline, which means it will terminate successfully right away. The most naive solution uses a call to getchar after scanf to eat that newline:

scanf("%d",&n);
getchar();
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.