This is the strcat function that I have implemented, but I get a segmentation fault when I go to the line *dst++ = *src++;. I have incremented src till '\0' as I want to append the next string starting from there. Can you please tell me why it gives segmentation fault? Am I doing any logical mistake by doing *dst++ = *src++;?
char *strconcat(char *dst, char *src)
{
char *fdst;
fdst = dst;
if (dst == '\0' || src == '\0')
return fdst;
while (*dst != '\0')
dst++;
while (*src != '\0')
*dst++ = *src++;
return fdst;
}
Hey i went through many solution given below and i made following changes but still i get segmentation problem when i start concatenate two strings, here is my entire code
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char *strconcat(char *dst, const char *src);
void main()
{
char *str1 = "Stack";
char *str2 = "overflow";
printf("MY CONCAT:%s\n",strconcat(str1, str2));
printf("INBUILT CONCAT:%s\n",strcat(str1, str2));
}
char *strconcat(char *dst, const char *src)
{
char *fdst;
int dst_len = 0, src_len = 0;
dst_len = strlen(dst);
src_len = strlen(src);
fdst = (char *) malloc(dst_len + src_len + 1);
fdst = dst;
if (src == NULL)
return fdst;
while(*dst)
{
dst++;
fdst++;
}
while(*src)
*fdst++ = *src++;
*fdst = '\0';
return fdst;
}
dstlarge enough to contain bothdstandsrc.