Thread: Measuring string length

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    10

    Measuring string length

    Am I just imagining something, or is there a strlen command? If there is how do I use it?

    I want to be able to measure the length of a string.
    Go you big red fire engine!

  2. #2
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    yes you can use
    Code:
    #include <cstring>
    using std::strlen;
    for c style strings

    or you can do something like

    Code:
    string s;
    s.length();

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    156
    I think you're going to need
    using std::string;
    for it to recognize string in the std namespace.

    dan

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    10
    I'm using visual c++ 6.0.

    Can I please have a bit more of an explaination, or a small example?

    Thankyou.
    Go you big red fire engine!

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    156

    Measuring string length

    The following was created in Visual C++ as a Win32 console applicaiton. Selecting "Typical Hello world application" then modifying it.

    Code:
    #include "stdafx.h" //for precompiled headers
    #include <iostream> //for cout and other c++ methods within iostream class
    #include <string> //for string class
    
    using namespace std; //use entire std namespace avoiding "std::" scope resolution operator
    
    int main(int argc, char* argv[])
    {
    
    	string sData = "Test";
    	cout << "sData contains " << sData << endl;
    	cout << "sData's length is " << sData.length( ) << endl;
    //or
    	int nStringLength = sData.length( );
    	cout << "sData contains " << sData << endl;
    	cout << "sData's length is " << nStringLength << endl;
    
    	return 0;
    
    }
    I hope this helps

  6. #6
    Unregistered
    Guest
    hmm. I figured a method out the other day.

    #include "iostream.h"
    #include "stdio.h" // Used for gets()

    int main()
    {
    char String[80];
    cout << "Enter a string ";
    gets(String);

    int idx = 0;
    while(String[idx] != NULL){ // you could replace NULL with 0
    idx++;
    }

    cout << "You typed " << idx << " characters/spaces".
    }

    I'm not sure if this was the answer you were looking for.

  7. #7
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    Not Hungarian notation (:

    Here is the c style one

    Code:
    #include <cstring>
    using std::strlen;
    
    #include <iostream>
    using std::cout;
    using std::endl;
    
    int main(void)
    {
         char* s = "Hello World!";
         int n = strlen(s);
         cout << "Hello World! is " << n << " charecters long" << endl;
         return 0;
    }

  8. #8
    0x01
    Join Date
    Sep 2001
    Posts
    88

    knave

    forgot to put my name up there.
    In the Unregistered post.

  9. #9
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    gets is dangourous and shouldn't be used.
    80 is a pretty good size buffer but someone still
    could enter 81 charectors possibly causing a stack overflow.
    Using fgets is safer.

  10. #10
    BubbleMan
    Guest

    Post This should work

    #include <iostream.h>
    #include <string.h>

    int main() {

    char string[600];

    cout << "Enter a string: ";
    cin.getline(string,600);
    cout << "Length: " << strlen(string);

    return 0;
    }

  11. #11
    Registered User
    Join Date
    Sep 2001
    Posts
    10
    Thankyou for all the help so far, but I still have a problem.

    I've been working with dangs code. His code works when it's written like it's shown above, but not when I modify the includes to how they are in my program.

    Here are the files that I am including in my program(MS VC++ 6):

    #include <iostream.h>
    #include <string.h>
    #include <stdlib.h>
    #include <fstream.h>
    #include <ctype.h>
    #include <iomanip.h>

    When they have the .h on the end dangs code doesn't work, but when i get rid of the .h, hundreds of other errors occur.

    I'm not sure why this occurs as I was under the impression that you didn't really need the .h in VC++ anymore (though I have been told to use it - which is why I do). Any help or suggestions will be greatly appreciated, thanks again for all your help so far.
    Go you big red fire engine!

  12. #12
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    You arn't supposed to have .h after std includes
    in c++. The reason why you having trouble
    is that <string.h> and <string> are different files.

  13. #13
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    change your includes from :-

    #include <iostream.h>
    #include <string.h>
    #include <stdlib.h>
    #include <fstream.h>
    #include <ctype.h>
    #include <iomanip.h>

    to:-

    #include <iostream>
    #include <cstring>
    #include <cstdlib>
    #include <fstream>
    #include <cctype>
    #include <iomanip>
    // and add this line directly underneath the includes...
    using namespace std;


    also if you are using the STL string you will need to add <string> to that list of includes...
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  14. #14
    Registered User
    Join Date
    Sep 2001
    Posts
    10
    Thankyou very much - all complete (except for some stupid memory error, but that's not related to this issue).
    Go you big red fire engine!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 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. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. ........ed off at functions
    By Klinerr1 in forum C++ Programming
    Replies: 8
    Last Post: 07-29-2002, 09:37 PM