How to read string with whitespace in C++?

string
whitespace

#1

Hi, I just want to know how I can get a string from stdin which contains whitespaces?
I tried fgets and scanf("%[^
]",str) but it’s still not working


String with spaces
#2

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 ’
’ 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.


#3

use gets()


#4

use getline() function


#5

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)


#6

#include<stdio.h>

let an array char b[53];

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


#7

I always use

int main()

{

char string[100];
printf("Enter whole sentece (yeah, bring spaces too, I can handle it):

“);
scanf (”%[^
]%*c", string);
printf (“You entered: %s”, string);
return 0;
}

Works every time!!


#8

Use the following:

string s;
getline(cin,s);


#9

TO SCAN STRING ENDING WITH NEWLINE CHARACTER : SCAN STRING WITH SPACE WORK EVERY TIME I TRUST ON IT

scanf ("%[^
]%*c", str);


#10

Use scanf("%[^
]%*c",string);


#11

Use std::cin and getline each time appending a each time.


#12

string s;
getline(cin,s);


#13

If you want to input many strings in one line with white spaces then you can try the following code:-

#include<bits/stdc++.h>
using namespace std;
int main()
{
    string s,out;
    stringstream ss;
    getline(cin,s);
    ss<<s;
    while(ss>>out)
    {
        cout<<out<<endl;
    }
    return 0;
}

But if the input consists of many integers (the number of integers being unspecified) then try the following code :-

#include<bits/stdc++.h>
using namespace std;
int main()
{
    string s,out;
    stringstream ss;
    getline(cin,s);
    ss<<s;
    int x;
    while(ss>>out)
    {
        stringstream convert(out);
        convert>>x;
        cout<<x<<endl;
    }
    return 0;
}

#14

you can use gets() or getline function to input instead of scanf()


#15

You can input string with white spaces by simply using ***scanf(" %[^
]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.


#16

Since strings are not built-in data type in C++, So we use #include<string> 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);


#17

Use getline(cin,s).
If s contains integers use atoi() function in


#18

Use below

std::cin >> noskipws;

Now reading from standard input will keep all whitespace characters as well.


#19

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
character encountered previously.Between consecutive getline(), do not use cin.ignore().


#20

scanf ("%[^ ]%*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