Do some research on strcat.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
>>Use two strings provided in main and then the function is called to add the rwo strings.
That's what strcat() does when using C style strings. Do you mean you need to write your own version of strcat() for educational purposes, that is, you are not allowed to used strcat()?
Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
Then think about the task. Say I give you one word with 4 letters and another word with 3 letters and I want you to create a new word by attaching the 3 letter word to the end of the 4 letter word. How much memory is the new word going to require? Do you know how to assign memory if you don't know how much memory you are going to require until run time? Do you know how to loop through a word looking at each char of the word and how to assign one char to another? If so, you can probably write a function to do the task. There may be other ways, too, but this always seemed the most straightforward way.
Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
Creating your own strcat is nothing, it takes literally a couple of lines.
void my_strcat(char *dest, char *source) {
// Find null-terminator
while ( *++dest );
// Add new string
while ( *dest++ = *source++ );
}
Making it safe would be the next problem. But think about whatLerner is telling you, you're assuming what the user inputs will be shorter than 128 characters, if you can't use std::strings, try learning how to use dynamic memory, or validating your input.
William Hemsworth
Posting Virtuoso
1,591 posts since Mar 2008
Reputation Points: 1,429
Solved Threads: 129