If i give space in my input it does not shows next words | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

If i give space in my input it does not shows next words

#include <stdio.h> int main() { char a[100]; scanf("%s",a); printf ("you entered: %s",a); return 0; }

26th Mar 2023, 6:35 AM
Adil Shaikh
7 Answers
+ 7
String is an array of characters in C so there is no need to put & before array a in scanf scanf("%s", a) ; edit: %s accepts only single word. To accept line of string, use fgets()
26th Mar 2023, 6:41 AM
Jayakrishna 🇮🇳
+ 4
You can use fgets() to read input string with spaces included. scanf() can also do something similar, but it's a little bit more tricky. http://www.cplusplus.com/reference/cstdio/fgets/
26th Mar 2023, 7:08 AM
Ipang
+ 4
Adil Shaikh Don't edit the original question. You can make another question instead.. Otherwise it confuses the readers.. and answer may not match as mine. %s accepts single word only... To accept entire line of string, use fgets() function or by scanf as scanf("%[^\n]", a); // accepts entire line (edited) Or fgets (a, 100,stdin) ; // input into array a, max length 100 from stdin. #include <stdio.h> int main() { char a[100]; scanf("%[^\n]",a); printf ("you entered: %s",a); return 0; } edited..
26th Mar 2023, 9:13 AM
Jayakrishna 🇮🇳
+ 3
scanf("%[^\n]", a); .....this will allow you to enter a single line of text until it find a new line character (when you press "enter"). scanf("%[^*]", a); .... this will allow you to enter multi line text until if find a " * " and then you press "enter".
26th Mar 2023, 9:54 AM
rodwynnejones
rodwynnejones - avatar
0
Otherwise use for loop
28th Mar 2023, 3:26 AM
Suraj
Suraj - avatar
0
H
28th Mar 2023, 4:29 AM
Herobrine
Herobrine - avatar
0
What?
28th Mar 2023, 5:11 AM
Suraj
Suraj - avatar