How to read string with whitespace in C++?

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

3 Likes

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.

3 Likes

use gets()

use getline() function

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)

2 Likes

#include<stdio.h>

let an array char b[53];

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

1 Like

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!!

6 Likes

Use the following:

string s;
getline(cin,s);

14 Likes

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

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

1 Like

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

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

string s;
getline(cin,s);

2 Likes

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;
}
6 Likes

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

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.

1 Like

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);

3 Likes

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

Use below

std::cin >> noskipws;

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

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().

21 Likes

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