length (arrays)

This is a discussion on length (arrays) within the C++ Programming forums, part of the General Programming Boards category; Hey guys, is there a length function I could use to work out the length of some things in a ...

  1. #1
    182
    182 is offline
    Registered User
    Join Date
    Oct 2005
    Posts
    133

    length (arrays)

    Hey guys, is there a length function I could use to work out the length of some things in a char array for example if in the array it held, "john smith" and I wanted to find out how many chars are in the surname?

    Thanks.

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,066
    Nope.

    Writing one yourself should be easy enough, though. Or you could do like all the real programmers do and use the proper std::string object. But hey, who wants to do that? Conformists suck.
    Last edited by SlyMaelstrom; 02-27-2006 at 08:17 AM. Reason: Minor syntax changes
    Sent from my iPadŽ

  3. #3
    182
    182 is offline
    Registered User
    Join Date
    Oct 2005
    Posts
    133
    Was just doing a bit of research and found strlen() couldnt this be used?

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,066
    No, because that gets you the entire array's length. Which is great, but you said you want the length of part of the array. As I said, it's easy enough to implement using strlen() among a few other functions, but what's the point? There is a standard C++ string object that does all of this for you.

    One day you're going to have to get over the fact that this is C++, not C.
    Sent from my iPadŽ

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,794
    You can of course use the strlen function to find the length of null-terminated character arrays. If you want to find the length of specific portions of that, for example the surname of "smith" in the array "john smith", then you can still use strlen but you need to use a pointer to the character after the space character (the 's' in this case)... or the original pointer plus an offset.

    Code:
    char * name = "john smith";
    cout << "Total length: " << strlen(name) << endl;
    char * ptr = strchr(name,' ');  // Find the space character
    if( ptr )
        cout << "Surname length: " << strlen(++ptr) << endl;
    Output:
    Code:
    Total length: 10
    Surname length: 5
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,451
    strlen() doesnt understand the concept of "surname".
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    182
    182 is offline
    Registered User
    Join Date
    Oct 2005
    Posts
    133
    Quote Originally Posted by hk_mp5kpdw
    You can of course use the strlen function to find the length of null-terminated character arrays. If you want to find the length of specific portions of that, for example the surname of "smith" in the array "john smith", then you can still use strlen but you need to use a pointer to the character after the space character (the 's' in this case)... or the original pointer plus an offset.

    Code:
    char * name = "john smith";
    cout << "Total length: " << strlen(name) << endl;
    char * ptr = strchr(name,' ');  // Find the space character
    if( ptr )
        cout << "Surname length: " << strlen(++ptr) << endl;
    Output:
    Code:
    Total length: 10
    Surname length: 5
    Hi Thanks for your advice that is what i have to do but if in teh array there was a middle name for example "john james smith" how can this find the surname if your not sure if all people will have a middle name or not? Thanks again

  8. #8
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,066
    Quote Originally Posted by 182
    Hi Thanks for your advice that is what i have to do but if in teh array there was a middle name for example "john james smith" how can this find the surname if your not sure if all people will have a middle name or not? Thanks again
    Do you live in a country where thinking is illegal? Seriously, what do you want from the guy? Should he just write the whole thing for you?
    Sent from my iPadŽ

  9. #9
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Start at the end of the array and work your way backwards until you find a ....?

    If you were using string types you could use rfind(), which does exactly that.

  10. #10
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    strlen() doesnt understand the concept of "surname".
    What?!! This is what it says at cppreference:

    #include <string.h>
    size_t strlen( char *str, char* surname);

    The strlen() function returns the length of str (determined by the number of characters before null termination). Or, if the optional second parameter is specifed, strlen() returns the length of any surname contained in str.

  11. #11
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Oh, 7stud, you're killing me!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. Strange string behavior
    By jcafaro10 in forum C Programming
    Replies: 2
    Last Post: 04-07-2009, 07:38 PM
  3. Arbitrary length multi-dimensional arrays
    By thw in forum C Programming
    Replies: 4
    Last Post: 11-22-2006, 01:25 PM
  4. turning strings of unknown length into arrays
    By hankspears in forum C Programming
    Replies: 3
    Last Post: 04-11-2002, 12:16 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21