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.
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.
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 09:17 AM. Reason: Minor syntax changes
Sent from my iPadŽ
Was just doing a bit of research and found strlen() couldnt this be used?
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Ž
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.
Output: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;
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
strlen() doesnt understand the concept of "surname".
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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 againOriginally Posted by hk_mp5kpdw
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?Originally Posted by 182
Sent from my iPadŽ
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.
What?!! This is what it says at cppreference:strlen() doesnt understand the concept of "surname".
#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.
Oh, 7stud, you're killing me!!!