how to find length of a string

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2006
Posts: 6
Reputation: tsubasa is an unknown quantity at this point 
Solved Threads: 0
tsubasa tsubasa is offline Offline
Newbie Poster

how to find length of a string

 
0
  #1
Apr 3rd, 2006
i want to find the length of a string but by using string.length() or string.size() well i don't really get the length of the string coz if i have a space well it won't read further. Eg "Hello world!!!" will return me 5 coz it will stop at the space. Can anyone help me on how i can find the length of a string???
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,707
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 275
Lerner Lerner is offline Offline
Posting Virtuoso

Re: how to find length of a string

 
0
  #2
Apr 3rd, 2006
Inputting material into the string will stop at whitespace characters such as space if you use the << operator to assign the information, but the string can have whitespace char if you initialize the string or if you use something like getline(). Once the string variable has a information that has whitespace in it the whitespace should be counted by either strlen() for null terminated strings or length() for STL strings. This should help illustrate the point.

char str[20] = "hello world";
cout << strlen(str) << endl;

std::string str1 = "hello world";
cout << str1.length() << endl;
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 6
Reputation: tsubasa is an unknown quantity at this point 
Solved Threads: 0
tsubasa tsubasa is offline Offline
Newbie Poster

Re: how to find length of a string

 
0
  #3
Apr 3rd, 2006
thanks a lot for your help!!!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC