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

int main()
{
  int N;
  int i;

  scanf("%d",&N);
  char A[100][100];

  for(i=0;i<N;i++)
    gets(A[i]);

  for(i=0;i<N;i++)
    printf("%s\n",A[i]);
}

Here is a simple program to enter a no of strings and then print them.When i use gets it reads one string less

Gergo Erdosi
  • 40,904
  • 21
  • 118
  • 94

1 Answers1

0

You need to clean the buffer after the scanf call because there is a \n stored in it (because you need to press enter to input the number). You can do it with:

char clean; 
while (clean=getchar()!='\n' && clean !=EOF);

this will keep reading the stdin buffer till it's clear.

also avoid using gets, it can cause a buffer overflow, use fgets instead and set the stream to stdin.

the code will be:

int N;
int i;
int clean;
scanf("%d", &N);
while (clean=getchar()!='\n' && clean !=EOF);
char A[100][100];
for (i = 0; i < N; i++)
    fgets(A[i],100,stdin);
for (i = 0; i < N; i++)
    printf("%s\n", A[i]);
sir psycho sexy
  • 780
  • 7
  • 19