Thread: Length of a string

  1. #1

    Angry Length of a string

    I am working on a challenge, and I need to find out how many characters are in a string. I have tried size() and length() but they both give me an error (undeclared identifier). But aren't those two functions in the string header? Also, what is the differences between the two headers cstring and string?


    EDIT: Oh, and I am using MSVC++6

  2. #2
    12938451357
    Guest
    You need to specify what you mean by "string". There's two kinds of strings.
    Code:
    // C++ strings
    #include <string>
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        string s("a string");
    
        cout<< s.length() <<endl;
    }
    Code:
    // C strings
    #include <cstring>
    #include <iostream>
    
    int main()
    {
        char s[] = "a string";
    
        cout<< strlen(s) <<endl;
    }
    Also, what is the differences between the two headers cstring and string?
    cstring works with C strings, it's the namespace'd version of C's string.h header file. string is the C++ header that contains the std::string class and all of it's necessary functions.

  3. #3
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    >> cstring works with C strings

    C strings being character arrays (namely char* or char[]).
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  4. #4
    I ment a string (the kind declared in string...not a character array). So I just guessed you could figure it out.

  5. #5
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Originally posted by Munkey01
    I ment a string (the kind declared in string...not a character array). So I just guessed you could figure it out.
    Code:
    #include <string>
    #include <iostream>
    
    using namespace std;
    
    int main(int argc, char** argv)
    {
         string message("Hello World!");
         cout << message << "\tmessage is " << message.size() << " characters long!\n";
         return 0;
    }

  6. #6
    Registered User
    Join Date
    Jul 2003
    Posts
    11
    >So I just guessed you could figure it out.
    If size() and length() didn't work then I had to assume you were doing something wrong or not using the std::string. Most likely your problem is that you're not using namespaces correctly. Try adding a
    Code:
    using namespace std;
    directive in your code if it's not there and see how it works. Usually I'll get an error saying that so and so is an undeclared identifier if I forget to resolve it from the std namespace.

  7. #7
    No, acually my problem was that I wasn't thinking of size() or length() has member functions. I was using them like-
    Code:
    for (int i=0; i<size(sentence); i++)
    //and
    for (int j=0; j<length(sentence); j++)
    But your code example showed me what I was doing wrong.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Calculating the length of a string
    By BSmith4740 in forum C Programming
    Replies: 32
    Last Post: 07-02-2008, 12:51 PM
  2. C++ FTP class won't work
    By lord mazdak in forum C++ Programming
    Replies: 8
    Last Post: 12-18-2005, 07:57 AM
  3. Weird modification to string length
    By ChwanRen in forum C Programming
    Replies: 0
    Last Post: 08-17-2003, 10:45 AM
  4. Basic C Programming Help Needed
    By Smurphygirlnz in forum C Programming
    Replies: 8
    Last Post: 09-26-2002, 07:12 PM
  5. string handling
    By lessrain in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 07:36 PM