×

# How to read string with whitespace in C++?

 2 2 Hi, I just want to know how I can get a string from stdin which contains whitespaces? I tried fgets and scanf("%[^\n]",str) but it's still not working asked 22 Feb '14, 19:34 2★ghaieth 56●2●3●11 accept rate: 0%

 4 I always use int main() { char string[100]; printf("Enter whole sentece (yeah, bring spaces too, I can handle it):\n"); scanf ("%[^\n]%*c", string); printf ("You entered: %s", string); return 0;  } Works every time!! answered 25 Feb '14, 06:36 4★roman28 1.6k●7●14●29 accept rate: 19%
 4 If you want to input many strings in one line with white spaces then you can try the following code:- #include using namespace std; int main() { string s,out; stringstream ss; getline(cin,s); ss<>out) { cout< using namespace std; int main() { string s,out; stringstream ss; getline(cin,s); ss<>out) { stringstream convert(out); convert>>x; cout<
 1 TO SCAN STRING ENDING WITH NEWLINE CHARACTER : SCAN STRING WITH SPACE WORK EVERY TIME I TRUST ON IT scanf ("%[^\n]%*c", str); answered 07 Jun '15, 08:45 1.0k●12●29 accept rate: 6%
 1 Since strings are not built-in data type in C++, So we use #include use string class in c++ program . We declare string variable as string s; and take input as 'cin>>s' but this will only take string upto first white-space. In order to get whole string with white-spaces we use string input function getline as string s; getline(cin,s); link This answer is marked "community wiki". answered 14 Jun, 21:20 1 accept rate: 0%
 0 Here is what i do : Use gets to input the string. If there was a scanf statement before the gets the use getchar to consume the extra '\n' character that remains in the input buffer. ex. Input 1) variable - t test cases 2) string 3) some integer input 2 and 3 repeat t times.  My approach: 1) scanf( %d ...) 2) getchar() 3) gets(My_string) 4) scanf(...) Repeat steps 2-4 t times.  Its a crude approach but works in most questions. answered 22 Feb '14, 19:45 3★kcahdog 10.0k●28●54●129 accept rate: 14% 1 in the first scanf, I usually write it as scanf("%d\n", ...) to trash the newline character, there's no need for extra getchar and also safer just in case there's some white space next to t :) (22 Feb '14, 20:44)
 0 use gets() answered 22 Feb '14, 20:19 0★sj_nita 1 accept rate: 0%
 0 use getline() function answered 22 Feb '14, 22:34 1 accept rate: 0%
 0 In GCC you can use scanf("%10[0-9a-zA-Z ]s", str); or scanf("%10[0-9a-zA-Z ]", str); It probably wont work with other compiler, but most people in coding contests use either gets(str) or getline(cin, str) answered 23 Feb '14, 00:34 575●1●5●11 accept rate: 13%

# include<stdio.h>

let an array char b[53];

you should do fflush(stdin); fgets(b,51,stdin);

306
accept rate: 0%

 0 Use the following: string s; getline(cin,s); answered 25 Feb '14, 09:34 574●4●7●12 accept rate: 0%
 0 Use scanf("%[^\n]%*c",string); link This answer is marked "community wiki". answered 11 Jan, 17:34 2 accept rate: 0%
 0 Use std::cin and getline each time appending a \n each time. answered 11 Jan, 18:18 93●5 accept rate: 9%
 0 string s; getline(cin,s); answered 11 Jan, 18:46 1 accept rate: 0%
 0 you can use gets() or getline function to input instead of scanf() answered 11 Jan, 20:24 0★prarthz 1 accept rate: 0%
 0 You can input string with white spaces by simply using scanf(" %[^\n]s",s);It will read string up to the newline character. Note the space before the '%' because it will flush all the whitespaces before the input string.If you will not put that then you will not get your desired result. answered 14 Jun, 19:30 3★divya_28 1 accept rate: 0%
 0 Use getline(cin,s). If s contains integers use atoi() function in answered 14 Jun, 21:36 1 accept rate: 0%
 0 Use below std::cin >> noskipws; Now reading from standard input will keep all whitespace characters as well. answered 14 Jun, 22:04 1 accept rate: 0%
 0 You have to use getline() as stated by all others. But if you have used cin before getline() then don't forget to use cin.ignore() and then use getline() because we have to remove white space or \n character encountered previously.Between consecutive getline(), do not use cin.ignore(). answered 14 Jun, 22:24 4★rj25 45●2 accept rate: 0%
 0 scanf ("%[^\n]%*c", str); Works unless you have overflown the limit like there is a question in HackerRank's 30 Day Challenge, where taking Double before string creats problem in getting a string with spaces link This answer is marked "community wiki". answered 15 Jun, 09:44 41 accept rate: 0%
 toggle preview community wiki:
Preview

By Email:

Markdown Basics

• *italic* or _italic_
• **bold** or __bold__
• image?![alt text](/path/img.jpg "title")
• numbered list: 1. Foo 2. Bar
• to add a line break simply add two spaces to where you would like the new line to be.
• basic HTML tags are also supported
• mathemetical formulas in Latex between \$ symbol

Question tags:

×418
×3

question asked: 22 Feb '14, 19:34

question was seen: 30,226 times

last updated: 15 Jun, 09:44