just started c++ (string)

i have just started learning c++(basically the strings).and today i came across a doubt(huge doubt)…Does a string terminates on whitespace.i have read that string terminates on occurence of ‘\0’ only.here is my program link.please tell me whats going on.why the output is not the whole string but a fragment of it.
thanks in advance

Use this :

Input : welcome to codechef

string s;

getline(cin,s);

getline considers whitespace too.

Hope this helps :slight_smile: Enjoy (Y)

1 Like

so is it also applicable with string under string.h because the result is same only the fragmentlink text

thank you so much for helping me :slight_smile: but one last query…what if we have to input two strings in a particular test case link to the question i was attempting here we have to input two strings in particular test case and if we use getline then it will take both the string in one go…so when to use getline and when to use cin. thanks in advance and sorry for asking too many questions

thanks a lot…but i dont understand why it gets terminated on occurence of whitespace.is there anything with the codechef’s compiler or it will give the same result with anyother.

does whitespace act as ‘\0’

char * terminates by null automatically when it faces a whitespace or newline on its way.

@Sneha : Yes :slight_smile:

char[] and string act as same in some extent.

string keyword defined in C++ only whereas char[] can be used in both C/C++.

Its okay… :slight_smile:
As per the question you referred it says to take two string with space seperated So there is no point to confuse.
simply take two string with cin.
Example : string s1,s2;
cin>>s1>>s2;