You are not logged in. Please login at www.codechef.com to post your questions!

×

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

ghaieth's gravatar image

2★ghaieth
562311
accept rate: 0%


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

link

answered 25 Feb '14, 06:36

roman28's gravatar image

4★roman28
1.6k71429
accept rate: 19%

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;
}
link

answered 11 Jan, 19:35

adityaghosh996's gravatar image

5★adityaghosh996
1075
accept rate: 0%

edited 11 Jan, 19:40

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

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

link

answered 07 Jun '15, 08:45

rcsldav2017's gravatar image

5★rcsldav2017
1.0k1229
accept rate: 6%

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

link
This answer is marked "community wiki".

answered 14 Jun, 21:20

vivekkumarbe01's gravatar image

2★vivekkumarbe01
1
accept rate: 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.

link

answered 22 Feb '14, 19:45

kcahdog's gravatar image

3★kcahdog
10.0k2854129
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) lazycoder974★

use gets()

link

answered 22 Feb '14, 20:19

sj_nita's gravatar image

0★sj_nita
1
accept rate: 0%

use getline() function

link

answered 22 Feb '14, 22:34

vivan9518s's gravatar image

1★vivan9518s
1
accept rate: 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)

link

answered 23 Feb '14, 00:34

c0d3junki3's gravatar image

3★c0d3junki3
5751511
accept rate: 13%

include<stdio.h>

let an array char b[53];

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

link

answered 24 Feb '14, 14:54

chomu1850's gravatar image

2★chomu1850
306
accept rate: 0%

Use the following:

string s; getline(cin,s);

link

answered 25 Feb '14, 09:34

predator0's gravatar image

2★predator0
5744712
accept rate: 0%

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

link
This answer is marked "community wiki".

answered 11 Jan, 17:34

naruto323's gravatar image

1★naruto323
2
accept rate: 0%

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

link

answered 11 Jan, 18:18

teracoder's gravatar image

2★teracoder
935
accept rate: 9%

string s; getline(cin,s);

link

answered 11 Jan, 18:46

varungoel252's gravatar image

2★varungoel252
1
accept rate: 0%

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

link

answered 11 Jan, 20:24

prarthz's gravatar image

0★prarthz
1
accept rate: 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.

link

answered 14 Jun, 19:30

divya_28's gravatar image

3★divya_28
1
accept rate: 0%

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

link

answered 14 Jun, 21:36

shubhamaggarw's gravatar image

2★shubhamaggarw
1
accept rate: 0%

Use below

std::cin >> noskipws;

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

link

answered 14 Jun, 22:04

pravar3088's gravatar image

2★pravar3088
1
accept rate: 0%

edited 14 Jun, 22:05

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

link

answered 14 Jun, 22:24

rj25's gravatar image

4★rj25
452
accept rate: 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

aashutosh001's gravatar image

3★aashutosh001
41
accept rate: 0%

toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • 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