tags:

views:

106

answers:

2

I tried to take input in form of string, specifically:

int i=0;
char c[50][500];
 for(;i<50;++i)
   scanf("%s",A[i]);

The input is

x  is a website that allows the easy[1] creation and editing of any number of interlinked 
 web pages via a web browser using a simplified markup language or a WYSIWYG text editor.

In my program, I want the text to be stored as:

A[0]="x  is a website that allows the easy[1] creation and editing of any number of interlinked"
A[1]=" web pages via a web browser using a simplified markup language or a WYSIWYG text editor."

But my code causes the text to be stored as

A[0]="is"
A[1]="a"
A[2]="website" 
....

What am I doing wrong?

+4  A: 

The scanf function scans whitespace-separated strings, not lines. Use fgets(A[i], sizeof(A[i]), stdin) instead.

Marcelo Cantos
+2  A: 

scanf with a %s will stop scanning when it encounters a space. To read by lines, use:

scanf("%[^\n]\n", A[i]);

To make sure that the length of the lines are within the allowed range, use:

scanf("%499[^\n]\n", A[i]);
Amarghosh