// Concatenate - concatenate two strings // with a " - " in the middle // (this version crashes) #include #include // prototype declarations void concatString(char szTarget[], char szSource[]); int main(int nArg, char* pszArgs[]) { cout << "This program concatenates two strings\n"; cout << "(This version crashes.)\n\n"; // read first string... char szString1[256]; cout << "Enter string #1:"; cin.getline(szString1, 128); // ...now the second string... char szString2[128]; cout << "Enter string #2:"; cin.getline(szString2, 128); // ...concatenate a " - " onto the first... concatString(szString1, " - "); // ...now add the second string... concatString(szString1, szString2); // ...and display the result cout << "\n" << szString1 << "\n"; return 0; } // concatString - concatenate the string szSource // onto the end of szTarget void concatString(char szTarget[], char szSource[]) { int nTargetIndex = 0; int nSourceIndex = 0; // find the end of the first string while(szTarget[nTargetIndex]) { nTargetIndex++; } // tack the second onto the end of the first while(szSource[nSourceIndex]) { szTarget[nTargetIndex] = szSource[nSourceIndex]; nTargetIndex++; nSourceIndex++; } }