how to calculate the size of a string constant in vc++

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vctiger
    New Member
    • Mar 2008
    • 2

    how to calculate the size of a string constant in vc++

    could someone tell me how to calculate the size of a string constant?Thanks . The following is my source code which contain few bugs in it.
    #include <iostream>
    #include <string>
    using namespace std;


    int main()
    {
    string s="hello world";
    int *ptr;
    int i=0;
    int length;



    while((*ptr)!=' \0')
    {
    ptr=s;
    i++;
    }
    length=i;
    cout<<"the length of string is"<<length<<"c haracters"<<end l;

    return 0;
    }
    can someone debug it for me?thanks!
  • oler1s
    Recognized Expert Contributor
    • Aug 2007
    • 671

    #2
    Your code is utterly broken, and writing all of that was unnecessary. You get the length of a C++ string with s.length() . See one possible reference page.

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      Originally posted by oler1s
      You get the length of a C++ string with s.length()
      Actually, string:: length() is deprecated. You should bs using string::size() to be consistent with the other STL containers.

      Comment

      • vctiger
        New Member
        • Mar 2008
        • 2

        #4
        Hi oler1s and weaknessforcats :
        Thanks for you guy's information. I have correctly rewrite the code and it works. Thank a lots.

        have a good weekend
        regards.

        Comment

        Working...