How to Manipulate Strings with Character in C++ - dummies

How to Manipulate Strings with Character in C++

By Stephen R. Davis

An array is a sequence of variables in C++ that shares the same name and that is referenced using an index. The following Concatenate program inputs two strings from the keyboard and concatenates them into a single string:

// Concatenate - concatenate two strings
//               with a " - " in the middle
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;
// prototype declarations
void concatString(char szTarget[], const char szSource[]);
int main(int nNumberofArgs, char* pszArgs[])
    // read first string...
    char szString1[256];
    cout << "Enter string #1:";
    cin.getline(szString1, 128);
    // the second string...
    char szString2[128];
    cout << "Enter string #2:";
    cin.getline(szString2, 128);
    // ...concatenate a " - " onto the first...
    concatString(szString1, " - ");
    // add the second string...
    concatString(szString1, szString2);
    // ...and display the result
    cout << "n" << szString1 << endl;
    // wait until user is ready before terminating program
    // to allow the user to see the program results
    cout << "Press Enter to continue..." << endl;
    cin.ignore(10, 'n');
    return 0;
// concatString - concatenate the szSource string
//                onto the end of the szTarget string
void concatString(char szTarget[], const char szSource[])
    // find the end of the first string
    int targetIndex = 0;
    // tack the second onto the end of the first
    int sourceIndex = 0;
        szTarget[targetIndex] =
    // tack on the terminating null
    szTarget[targetIndex] = '';

The Concatenate program reads two character strings and appends them together with a ” – “ in the middle.

The program begins by reading a string from the keyboard. The program does not use the normal cin >> szString1 for two reasons. First, the cin >> operation stops reading when any type of whitespace is encountered. Characters up to the first whitespace are read, the whitespace character is tossed, and the remaining characters are left in the input hopper for the next cin >> statement.

Thus, if you were to enter “the Dog”, szString2 would be filled with “the” and the word “Dog” would be left in the input buffer.

The second reason is that the getline() allows the programmer to specify the size of the buffer. The call to getline(szString2, 128) will not read more than 128 bytes no matter how many are input.

Instead, the call to getline() inputs an entire line up to but not including the newline at the end.

After reading the first string into szString1[], the program appends ” – “ onto the end by calling concatString(). It concatenates the second string by calling concatString() with szString2[].

The concatString() function accepts a target string, szTarget, and a source string, szSource. The function begins by scanning szTarget for the terminating null character, which it stores in targetIndex. The function then enters a second loop in which it copies characters from the szSource into szTarget starting at the terminating null. The final statement in concatString() slaps a terminating null on the completed string.

An example output from the program appears as follows:

Enter string #1:this is a string
Enter string #2:THIS IS A STRING
this is a string - THIS IS A STRING
Press Enter to continue...