You have a number of not just quite right beginning to each of your functions. Firstly, let’s think about the returns for each. myStrlen
should return size_t
instead of int
. C++ designates a size_type for counters, measuring, etc.. The remaining functions should return char*
(or nullptr
on failure).
Looking at your myStrlen
function where you have
for (i=0; str1[i] != '\0'; i++)
str1[i] = '\0';
You are setting every character in str1
to the nul-character because you are applying the loop to the next expression. You should not be worrying about nul-terminating anything within myStrlen
— you are just counting characters. So you can rewrite it as follows:
size_t myStrlen (const char *str)
{
size_t l = 0;
for (; str[l]; l++) {}
return l;
}
Your myStrcpy
looks workable, though you should always validate your input parameters are not nullptr
before using them — I leave that to you. Since you have a myStrlen
function, you can simply use that along with memcpy
to create your myStrcpy
function as:
char *myStrcpy (char *dest, const char *src)
{
size_t len = myStrlen(src);
return (char *)memcpy (dest, src, len + 1);
}
(note: traditionally you have source (src
) and destination (dest
) parameters when copying or concatenating)
For your myStrcat
function, you are just using the myStrlen
function to find the offset in dest
to append src
, so you really just need a call to myStrlen
and then a call to myStrcpy
to copy src
to that offset in dest
, e.g.
char *myStrcat (char *dest, const char *src)
{
size_t len = myStrlen (dest);
return myStrcpy (dest + len, src);
}
In your main()
, if you want a space between "Hello"
and "World"
, then const int SIZE = 11;
is one too-low to hold the concatenated string "Hello World"
which would require 12-bytes
(including the nul-terminating character). Do Not Skimp on buffer size. 128
is plenty small.
Remaining with your main()
but updating SIZE = 12;
and adding a space between "Hello"
and "World"
with an additional call to myStrcat
, you could do the following:
int main (void)
{
const int SIZE = 12; /* too short by 1 if you add space between */
char s1[SIZE] = "Hello";
char s2[SIZE] = "World";
std::cout << "s1: " << " " << s1 << std::endl << std::endl;
std::cout << "The length of s1: " << myStrlen(s1) << std::endl << std::endl;
std::cout << "Doing strcat(s1, s2) " << std::endl;
myStrcat(s1, " ");
myStrcat(s1, s2);
std::cout << "s1: " << " " << s1 << std::endl;
std::cout << "The length of s1: " << myStrlen(s1) << std::endl << std::endl;
std::cout << "Doing strcpy(s1, s2) " << std::endl;
myStrcpy(s1, s2);
std::cout << "s1: " << " " << s1 << std::endl;
std::cout << "The length of s1: " << myStrlen(s1) << std::endl << std::endl;
}
(note: don’t include using namespace std;
, it is just bad form in this day and age)
Example Use/Output
$./bin/mystrcpy
s1: Hello
The length of s1: 5
Doing strcat(s1, s2)
s1: Hello World
The length of s1: 11
Doing strcpy(s1, s2)
s1: World
The length of s1: 5
Look things over and let me know if you have further questions.
solved String Functions: Strcat()