Once again, I need a push in the right direction.
The problem is to make a pointer program that copies one string onto the end of the other. I did this and it works perfectly. However, the 2nd part of the problem says if the first string doesn't have enough memory, to add what it can to the first string and null terminate it. Here's the program I wrote so far:
Code:
#include <stdio.h>
//This adds one string to the end of another
//Input: You need two pointers to two different strings
//Output: It doesn't return anything. However, it does add one string to the other
void _strcat(char * s_pointer, char * t_pointer)
{
int i = 0; //my two element counters
int j = 0;
while (*(s_pointer + i) != '\0') //finds the end of s
{
i++;
}
while ((*(s_pointer + i) = *(t_pointer + j)) != '\0') //copies t to s
{
i++;
j++;
}
return;
}
int main()
{
char s[15] = "Four score";
char t[] = " and seven years ago";
char * s_pointer = &s;
char * t_pointer = &t;
_strcat(s_pointer, t_pointer); //using my function
printf("%s\n", s); //output
return 0;
}
I figured the memory part will go in the 2nd while loop? But I don't know how to add extra memory. Right now, the program will crash. I'm using Four score and seven years ago as a test. I cannot simply put a bigger number in the s array index. Thanks for the help