Evening everyone, need your help with the following task:
I have to write overloading methods that will do ( strcpy, strlen, strcat, strstr, strchr, strcmp) for char arrays. ( Next lesson our teacher will introduce the string library and will get over char data type.)
I have to mention that the code should be done in a specific way , and it has to include constructors, set and get and all the basic info.
I've done the strcpy overloading method, however I need a hint or reference how to continue with other functions.
its all about finding the zero for C-strings.
you iterate from the beginning until you find a character that is literally the value 0 or if you want to type more letters, '\0'.
that tells you the length.
it tells you where to start copying into it for concatenation.
for strstr, you unfortunately need to pattern match the darn thing.
something like
for every letter in the target,
does that letter match the first letter of what you are looking for?
if it does, you can get snarky and use memcmp on the block of bytes or you can just iterate and check matches in a loop. If you found it, return the address of the first letter, if not, return 0 (null pointer).
strcmp is just a wrapper for memcmp or you can loop it here. find the lengths of both, if they are equal, then check the data, if not equal, early free exit (no match).
maybe that will get you started? None of these should be any harder than what you already have done here.
extra credit: you can cheat since its your own class and keep the size of the arrays in hand without seeking the zero all the time. There are 2 ways to do this: you can have an extra character and use the first location [0] as the length (0-255 chars) or you can have a new class member. If you use the first location, you have to remember to return &string[1] instead of just string each time you need to be compatible with C-strings. C++ allows negative indexing, so you can say mystring[-1] to recover it out in the wild. This is sort of going down a weird rabbit hole, but its been done this way (I think this idea is called a pascal string, from the language pascal which I think is dead now).
that will do ( strcpy, strlen, strcat, strstr, strchr, strcmp) for char arrays.
...
I've done the strcpy overloading method
I don't see any code that does a strcpy()? Are you required to provide your own versions of these c-string functions - or do you want need to wrap the existing standard functions into class methods?
If you are to provide your own versions of the c-string functions, then for simple versions consider (with a trailing 1 to avoid duplicate of the standard ones):