- #1
galaxy_twirl
- 137
- 1
Hi everyone. This is similar to my strcpy question previously. I tried to code but when I ran the code, a segmentation fault appears. I think this has something to do with my loop for the array but I have problems resolving it. May I have some help please? Thank you. My codes and the original question are below:
Question: Implement the my_strcat(s1,s2) function to append a copy of string s2, including the terminating null character, to the end of string s1. The initial character of s2 overrides the null character at the end of s1. String s1 is returned.
Use the function "char *my_strcat(char *s1, const char *s2);"
I think, after catting the 2 strings, I should get in C terms, gooseberry(\0). \0 is the last null char occupying the last box of the str, which is a char array.
Question: Implement the my_strcat(s1,s2) function to append a copy of string s2, including the terminating null character, to the end of string s1. The initial character of s2 overrides the null character at the end of s1. String s1 is returned.
Use the function "char *my_strcat(char *s1, const char *s2);"
Code:
#include <stdio.h>
char *my_strcat(char *s1, const char *s2);
int main(void)
{
char str1[40]={"goose"};
char str2[40]={"berry"};
char* s1;
char* s2;
char* type_berry;
type_berry = my_strcat(s1, s2);
printf("%s\n", *type_berry);
return 0;
}
char *my_strcat(char *s1, const char *s2)
{
int i=0, j=0, count=0;
while(s1[i] != '\0')
{
count = count + 1;
i++;
}
i = count;
while(s2[j]!= '\0')
{
s1[i]= s2[j];
i++;
j++;
}
return s1;
}
I think, after catting the 2 strings, I should get in C terms, gooseberry(\0). \0 is the last null char occupying the last box of the str, which is a char array.