-3

I have converted c++ to c type string and working with strlen but it is not working.

 #include<bits/stdc++.h>
using namespace std;
int main(){
string s("Hello");
s.c_str();
cout<<strlen(s);
}
Ishan Bansal
  • 99
  • 1
  • 8
  • Calling `s.c_str()` doesn't convert the `std::string` to a "c type string." It returns a pointer to the string held in the `std::string` which you ignore. So the statement, essentially, is a no-op. Try: `std::cout << strlen(s.c_str ())` or, better still, `std::cout << s.size()` – Nik Bougalis Sep 09 '15 at 19:45
  • 2
    This line `s.c_str();` does nothing. – crashmstr Sep 09 '15 at 19:45
  • 1
    It is.....not working? That's a pretty vague bug report. What's it supposed to do? What's it actually doing? –  Sep 09 '15 at 19:46
  • 3
    Congratulations! You just included the entire standard library AND pulled it all into the global namespace. I can't see this working well with anything non trivial. To explain: `#include` this includes the whole damn standard library or near enough to make no difference. That's a lot of extra compiling. `using namespace std;` makes everything in the std namespace global. You are setting yourself up for massive mystery bug nightmares with two lines of code. Don't do this to yourself. – user4581301 Sep 09 '15 at 19:51
  • 1
    @user4581301 We've got a [meme](http://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) for this recently :-D ... – πάντα ῥεῖ Sep 09 '15 at 20:03
  • @πάνταῥεῖ I'm just thrilled to see both [Wave Motion](https://www.youtube.com/watch?v=Kj4PSlqt4BQ)-calibre FUBAR cannons in use at the same time. The debugging will be epic. – user4581301 Sep 09 '15 at 20:09

3 Answers3

5
s.c_str();

This code has no side effects on s actually regarding a conversion or such. You want to process the result further.

Instead of

cout<<strlen(s);

you want to have either

cout<<strlen(s.c_str());

or

cout<<s.size();

Where the latter is the certainly more efficient, because the standard requires a time complexity of O(1) for std::string::size(), where strlen() cannot guarantee a better time complexity than O(strlen(s)).

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
4

If you want to waste clock-cycles in your processor:

 cout << strlen(s.c_str());

will count every character up to the first nul character in s.

If you just want to know how long the string is:

 cout << s.length();

or

 cout << s.size();

will give you that (and with an algorithm that is O(1) rather than the strlen algorithm that is O(n) - meaning that a million character long string takes one million times longer to measure the length of than a single character string). [Of course, if you have a std::string that contains a nul character in the middle, you may find that one of these methods is "right" and the other is "wrong" because it either gives too long or too short a length - a C style string is not allowed to contain a nul character in the middle of the string, it is only allowed as a termination]

Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
0

Why use strlen when the string class does the business?

Please read http://en.cppreference.com/w/cpp/string/basic_string and not the size/length method

Otherwise do as the @πάντα ῥεῖ suggests

Ed Heal
  • 59,252
  • 17
  • 87
  • 127