# Input string in C++

**URL:** <https://discuss.codechef.com/t/input-string-in-c/12418>\
**Category:** general\
**Tags:** c-plus-plus, strings\
**Created:** [March 31, 2016, 11:36am UTC](https://discuss.codechef.com/t/input-string-in-c/12418 "2016-03-31T11:36:29Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![jain\_mj](https://www.codechef.com/sites/all/themes/abessive/images/user_default_thumb.jpg) [@jain\_mj](https://discuss.codechef.com/u/jain_mj)\
**Post date:** [March 31, 2016, 11:36am UTC](https://discuss.codechef.com/t/input-string-in-c/12418/1 "2016-03-31T11:36:29Z")

</div>

How to input a string whose number of characters we do not know using scanf in c++ ??

I got this question is discuss by some user posted in answer column… so I thought to move it to Q&A for help.

---

<div class="post-metadata">

**Author:** ![jain\_mj](https://www.codechef.com/sites/all/themes/abessive/images/user_default_thumb.jpg) [@jain\_mj](https://discuss.codechef.com/u/jain_mj)\
**Post date:** [March 31, 2016, 11:45am UTC](https://discuss.codechef.com/t/input-string-in-c/12418/2 "2016-03-31T11:45:48Z")

</div>

@rajvir007 You dont need the string length before for taking input of a string through scanf() and thats the advantage of scanf() function. Just use it like this:

char str[1000];  
scanf("%s",str);

You can take input of a string of length upto 999 characters as last character is used for storing null character.

Also if you work in C++ which is far better and a good approach for beginners, you should use string data type like this:

string str;  
cin\>\>str;

Remember scanf() can’t take input of string data type.  
This has advantage of variable length you can have any length string input to it just only disadvantage is it is little slow compared to scanf().  
I have upvoted your question which was posted in answers so that you can now ask question on your own. 🙂 Happy to help 🙂

---

<div class="post-metadata">

**Author:** ![ash\_code](https://www.codechef.com/sites/all/themes/abessive/images/user_default_thumb.jpg) [@ash\_code](https://discuss.codechef.com/u/ash_code)\
**Post date:** [March 31, 2016, 11:56am UTC](https://discuss.codechef.com/t/input-string-in-c/12418/3 "2016-03-31T11:56:53Z")

</div>

Just use **scanf("%s", stringName);** or **cin \>\> stringName;**

**tip:** If you want to store the length of the string while you scan the string, use this : **scanf("%s %n", stringName, &stringLength);**

stringName is a character array/string and strigLength is an integer.  
Hope this helps. Happy Coding!

---

<div class="post-metadata">

**Author:** ![anon87739712](https://www.codechef.com/sites/all/themes/abessive/images/user_default_thumb.jpg) [@anon87739712](https://discuss.codechef.com/u/anon87739712)\
**Post date:** [March 31, 2016, 3:04pm UTC](https://discuss.codechef.com/t/input-string-in-c/12418/4 "2016-03-31T15:04:33Z")

</div>

Hello,  
scanf is faster than cin, but you could turn the iostream much faster just adding a simple line to your code

```
 std::ios::sync_with_stdio(false);

```

then the iostream is faster than scanf.

---

<div class="post-metadata">

**Author:** ![geek\_geek](https://www.codechef.com/sites/all/themes/abessive/images/user_default_thumb.jpg) [@geek\_geek](https://discuss.codechef.com/u/geek_geek)\
**Post date:** [April 1, 2016, 11:56am UTC](https://discuss.codechef.com/t/input-string-in-c/12418/5 "2016-04-01T11:56:09Z")

</div>

there are many ways to do this.

char s[1000];

cin\>\>s;

scanf(“%s”,&s);

gets(s);

The first two ways , there is a problem as it break the input at white spaces , meaning if the input string is “Hello World”, s takes the value “Hello”.

The third ways is safe , gets does not break at white spaces ,(all though it raises a compiler warning which you can ignore) and is faster than the first two methods.

Look into [this link](http://www.cplusplus.com/reference/cstdio/gets/)

---

<div class="post-metadata">

**Author:** ![ash\_code](https://www.codechef.com/sites/all/themes/abessive/images/user_default_thumb.jpg) [@ash\_code](https://discuss.codechef.com/u/ash_code)\
**Post date:** [March 31, 2016, 12:24pm UTC](https://discuss.codechef.com/t/input-string-in-c/12418/6 "2016-03-31T12:24:04Z")

</div>

What are you trying to do? You could have posted a comment easily. This is ridiculous.

---

<div class="post-metadata">

**Author:** ![adi96433](https://discuss.codechef.com/user_avatar/discuss.codechef.com/adi96433/32/113862_2.png) [@adi96433](https://discuss.codechef.com/u/adi96433)\
**Post date:** [November 8, 2020, 1:14pm UTC](https://discuss.codechef.com/t/input-string-in-c/12418/7 "2020-11-08T13:14:12Z")

</div>

> [@anon87739712](#):
>
> `std::ios::sync_with_stdio(false);`

the moment i added this line of code in my program it printed 0 0 0 which was earlier working fine ☹

---

<div class="post-metadata">

**Author:** ![anirudh09041](https://discuss.codechef.com/user_avatar/discuss.codechef.com/anirudh09041/32/209436_2.png) [@anirudh09041](https://discuss.codechef.com/u/anirudh09041)\
**Post date:** [April 7, 2021, 10:44am UTC](https://discuss.codechef.com/t/input-string-in-c/12418/8 "2021-04-07T10:44:57Z")

</div>

try not to use cin and scanf together when using std::ios::sync\_with\_stdio(false);  
it creates synchronization problem

---

<div class="post-metadata">

**Author:** ![anmit007](https://discuss.codechef.com/user_avatar/discuss.codechef.com/anmit007/32/27482_2.png) [@anmit007](https://discuss.codechef.com/u/anmit007)\
**Post date:** [April 7, 2021, 11:04am UTC](https://discuss.codechef.com/t/input-string-in-c/12418/9 "2021-04-07T11:04:23Z")

</div>

You can simply use **cin \>\> stringname** , you don’t need to know the number of characters.
