| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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 #include <stdio.h> #include <iostream.h> // the following include file is required for the str functions #include <string.h> // prototype declarations void concatString(char szTarget[], char szSource[]); int main (int nArg, char* pszArgs[]) { //read the first string char szString1[256]; cout << "Enter string #1: "; cin.getline(szString1, 128); //now get the second string char szString2[128]; cout << "Enter String #2: "; cin.getline(szString2, 128); //concatenate a "-" onto the first... (point1) concatString(szString1, " - "); //strcat (szString1, " - "); //now add the second string (point2) concatString(szString1, szString2); //strcat (szString1, szString2); //and display the result cout << "\n" << szString1 << "\n"; return 0; } //concatString - concatenate the szSource string on to the end of the szTarget //string void concatString(char szTarget[], char szSource[]) { // Find the end of the first string int targetIndex =0; while (szTarget[targetIndex]) // (point3) { targetIndex++; } //tack the second on to the end of the first int sourceIndex=0; while (szSource[sourceIndex]) // (point4) { szTarget[targetIndex] = szSource[sourceIndex]; targetIndex++; sourceIndex++; } //tack on the terminating null szTarget[targetIndex]='\0'; } 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 point onwards! I would be most grateful if someone could explain this to a newbie who is going to throw this textbook out of the window!!! Cheers, Mike. |
|
#2
|
|||
|
|||
|
This program concatenates 2 strings together with a " - " sign in between them. The function is first called to append the " - " on the end of the first string. Then the function is called again to append the second string on the end.
So if our strings are "first" and "second" then the function is first called with "first" and " - " so we have strings "first - " and "second". When it is called the second time we get "first - second" as the result. Hope this helps, -KM- |
|
#3
|
|||
|
|||
|
it does thanks!
|
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > concatenate two strings - newbie help! |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|