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

×

How to read string with whitespace in C++?

1
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
462311
accept rate: 0%


12next »

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

4★adityaghosh996
1094
accept rate: 0%

edited 11 Jan, 19:40

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%

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
9.9k2854129
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) lazycoder975★

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

4★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

3★predator0
5744712
accept rate: 0%

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

4★rcsldav2017
981726
accept rate: 7%

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

link
This answer is marked "community wiki".

answered 11 Jan, 17:34

naruto323's gravatar image

2★naruto323
1
accept rate: 0%

toggle 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

Tags:

×312
×2

Asked: 22 Feb '14, 19:34

Seen: 15,892 times

Last updated: 11 Jan, 20:24