August 8th, 2004, 11:10 AM
Join Date: Jun 2004
Time spent in forums: < 1 sec
Reputation Power: 0
concatenate two strings - newbie help!
Ive taken the following program from a textbook im working through....
// Concatenate - concatenate two strings with a " - " in the middle
// the following include file is required for the str functions
// prototype declarations
void concatString(char szTarget, char szSource);
int main (int nArg, char* pszArgs)
//read the first string
cout << "Enter string #1: ";
//now get the second string
cout << "Enter String #2: ";
//concatenate a "-" onto the first... (point1)
concatString(szString1, " - ");
//strcat (szString1, " - ");
//now add the second string (point2)
//strcat (szString1, szString2);
//and display the result
cout << "\n" << szString1 << "\n";
//concatString - concatenate the szSource string on to the end of the szTarget
void concatString(char szTarget, char szSource)
// Find the end of the first string
int targetIndex =0;
while (szTarget[targetIndex]) // (point3)
//tack the second on to the end of the first
while (szSource[sourceIndex]) // (point4)
szTarget[targetIndex] = szSource[sourceIndex];
//tack on the terminating null
I would be really grateful if someone could clarify the code ive marked in brackets as points 1-4.
I think I understand 80% of what this program is doing, I just cant work out how its doing it - if
that makes any sense? on point1, the line "concatString(szString1, " - ");" tells me that its going to
run the concatString function, using the values in szString1 and for some reason, a minus sign.
On point2, it seems to do exactly the same thing, though using the values of szString1 and szString2. Is it
running the concatString function twice, or am I cracking up?
On points 3 and 4, I cant see how the "while" aspects are working as there are no conditions? in the first
example, it sets targetIndex to 0 and then I simply do not understand what it is doing from that
I would be most grateful if someone could explain this to a newbie who is going to throw this textbook
out of the window!!!