0

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;

}

2
Contributors
1
Reply
3
Views
5 Years
Discussion Span
Last Post by Ancient Dragon
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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.