// Concatenate - concatenate two strings // with a " - " in the middle #include #include // the following include file is required for the // str functions #include // prototype declarations void concatString(char szTarget[], char szSource[]); int main(int nArg, char* pszArgs[]) { // 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, " - "); // strcat(szString1, " - "); // ...now add the second string... concatString(szString1, szString2); // strcat(szString1, szString2); // ...and display the result cout << "\n" << szString1 << "\n"; return 0; } // concatString - concatenate the szSource string // onto the end of the szTarget string void concatString(char szTarget[], char szSource[]) { // find the end of the first string int nTargetIndex = 0; while(szTarget[nTargetIndex]) { nTargetIndex++; } // tack the second onto the end of the first int nSourceIndex = 0; while(szSource[nSourceIndex]) { szTarget[nTargetIndex] = szSource[nSourceIndex]; nTargetIndex++; nSourceIndex++; } // tack on the terminating null szTarget[nTargetIndex] = '\0'; }