include<stdio.h>

int strlen_(const char *string);
int strlen_(const char *string)
{
int length;
for(length = 0; *string != '\0'; string++){
length++;
}
return length;
}

char *strcat_(char *string1, const char *string2);
char *strcat_(char *string1, const char *string2)
{
char *p = string1;
if(string1 == NULL || string2 == NULL)
return string1;

while(*string1 != '\0')
    string1++;

while(*string1++ = *string2++)
    ;

return p;

}

int main()
{
char *string1 = "my";
char *string2 = "me";
int num = strlen_(string1);

printf("the length before strcat is %d\n", num);
strcat_(string1,string2);
int num2 = strlen_(string2);
printf("the length after strcat is %d", num2);
printf("\nThis is it!\n");

return 0;

}

You can not concantinate string2 to the end of string1 because (1) string1 is a string leteral so most likely its stored in read-only memory, and (2) there is not enough room in string1 to contain both strings. In main() try this:

char string1[80] = "my";

The above allows you to copy up to 80 charcters in the string1 buffer, which is located in writable memory because it is not a string literal.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.