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

×

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

asked 14 Jan '15, 17:41

sneha6966's gravatar image

0★sneha6966
335
accept rate: 0%


Use this :

Input : welcome to codechef

string s;

getline(cin,s);

getline considers whitespace too.

Hope this helps :) Enjoy (Y)

link

answered 14 Jan '15, 17:49

nadimayaz's gravatar image

3★nadimayaz
119249
accept rate: 5%

edited 14 Jan '15, 17:54

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.

(14 Jan '15, 17:57) sneha69660★

does whitespace act as '\0'

(14 Jan '15, 17:57) sneha69660★

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

(14 Jan '15, 18:02) nadimayaz3★

@Sneha : Yes :)

(14 Jan '15, 18:05) nadimayaz3★

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

link

answered 14 Jan '15, 18:08

sneha6966's gravatar image

0★sneha6966
335
accept rate: 0%

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

(14 Jan '15, 18:13) nadimayaz3★

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

(14 Jan '15, 18:14) nadimayaz3★

thank you so much for helping me :) 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

link

answered 14 Jan '15, 18:57

sneha6966's gravatar image

0★sneha6966
335
accept rate: 0%

Its okay.. :) 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;

(14 Jan '15, 19:04) nadimayaz3★
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:

×354
×200

question asked: 14 Jan '15, 17:41

question was seen: 1,602 times

last updated: 14 Jan '15, 19:04