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???

Recommended Answers

All 2 Replies

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;

thanks a lot for your help!!! :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.