void Strcat(char str1[], char str2[]){
long len1 = strlen(str1);
long len2 = strlen(str2);
char* str = (char*)malloc(((len1 + len2) + 1)*sizeof(char));
if(str == NULL){
printf("No memory");
exit(1);
}
for (int i = 0 ; str1[i] != '\0'; i++) {
str[i] = str1[i];
}
str[strlen(str1)] = ' ';
for (long i = 0, j = strlen(str1)+1 ; str2[i] !='\0' ; i++, j++) {
str[j] = str2[i];
if(str2[i+1] == '\0')
str[j+1] = '\0';
}
//puts(str);
printf("strlen STR -> %ld\n", strlen(str));
for (int i = 0; str[i] != '\0'; i++) {
printf("%c",str[i]);
}
free(str);
}
Ok I know the strcat function is a string between two strings. Suppose I put the input "ttt" into the first string And the second string the input "yyy". I am now using dynamic assignment using malloc Now I know we need to take the length of the first + second + 1 the 1 is for the '\0' character.
So my allocation is size 7.
but I need to make a space between the two strings Do I need my allocation to be 8? because when I do only sizeLength + 1 the program is still working and it still puts a space between the two strings and I feel like the compiler forgives me.