-
Appending Char
Well I am having problems prety bad, want to make a function in which can concatenate one string to another. I have to do it with a pointer notation and a array notation
Code:
char* strcat(char *str, const char *str2)
{
}
Code:
char* strcat(char str1[], const char str2[])
{
}
Can anyone help me?
-
When you say strings do you mean the actual string class or an array of char?
If so, then why not use the append?
http://www.cppreference.com/cppstring/append.html
-
I meant to say char, and I am not allowed to use append I have already something in the lines of
Code:
char *result = str1;
while(*str1 != "/0")
{
str1++;
while( *str2 != "/0")
{
*str1 = str2;
str1++;
str2++;
}
}
return *result;
I dont think that wokrs though anyone can confirm that one for pointers? arrays I have no idea how I am going to do it
also its char, not strings sorry bout that
-
*double post*
Anyone anyone??
-
Your code is close. What you are trying to do is loop to the end of str1, then loop through str2 adding each of its characters to str1, right? Your first loop should end before the second loop starts so that the str2 characters will be tacked on at the end, but it doesn't in that code.
You also have a typo where you forgot to dereference the str2 pointer, and you need to make sure you add a '\0' to the end of str1 after you're done. Finally, I believe the methods should be identical for arrays or pointers.