Welcome guest! Click here to register or login.
logo

<< Tutorial 7
Tutorial 9 >>

Strings in C++

***Please register FREE to rate this tutorial***


Topics
Basics
The string library
Strings & Array's
Strings & Cin
Examples
Example 1
Example 2

Basics

Recall from tutorial 1 the progam which accepted a name from the user and displayed a message. Here is that program again:

#include <iostream>
#include <string>
using namespace std;

int main(){
    string name = "";

    cout << "Enter your first name: ";
    cin >> name;

    cout << "Hello " << name << endl;
    return 0;
}

The program simply accepts a string from the user and stores it in a string varible called name.

Also needed with the above program is the library called <string> that is built in to C++.

The string library

Let's consider the following program that shows numerous elements of the string library. The comments explain each part of the program.

Example 1:
String functions

Download source code here (Right click - Save Tagret As...)

#include <iostream>
#include <string>
using namespace std;

int main(){

string str = "Welcome to Tutorial 8!";

/*
1. The length() function OR the size()
function return an integer which is
the size of the string.
*/

cout << "Calling length(): "
     << str.length() << endl;
cout << "Calling size(): "
     << str.size() << endl;

/*
2. The find function will return an integer
representing the FIRST OCCURANCE of a certain
string, here the functions argument.
*/

cout << "Calling find(\"to\"):"
     << str.find("to") << endl;

/*
3. The substr() function "substring" will return a string
which starts and ends at a determined index(es), the
arguments of the function.  If only 1 argument is given,
it will begin at that index and continue all the way to the end.
*/

cout << "Calling substr(0,2): "
     << str.substr(0,2) << endl;
cout << "Calling substr(4): "
     << str.substr(4) << endl;

/*
4. The insert() function will look at a designated index
and insert the string, as the second parameter, in that
place.
*/

cout << str.insert(0,"HHH") << endl;

/*
5. The append() function will add the string argument
to the END of the string.
*/

str.append("----");
cout << str << endl;

return 0;
}

Here is a bit more detail about the above program in terms of its functions.

length() or size()

The length() and size() functions in the string library do the same job in terms of returning an integer representing the physical length of the string, meaning every character include the whitespaces.

find()

The find() function will take, as it's argument, a string and return the index of the FIRST OCCURANCE of that string as it appears in the str variable. Here, at index 8, the string begins to appear, so 8 will be returned by the function. In the event that the string is not found by the function, it will return a -1.

substr()

The substr() function will take either 1 or 2 arguments depending on how you wish to use it. If it takes 1 argument, that argument will represent the starting index and display (or return) the string from that index onward. It will nor return a string of a certain length but the rest of it. If the function is given 2 arguments, the first argument is the beginning index for the substring and the second one is the number of places you wish to move, INCLUDING THE STARTING POINT. So in the example above, the string "We" is returned since we started at index 0 and advanced 2 places.

insert()

The insert() function will take 2 arguments. The first argument is the index of the string you wish to insert something at and the second is the actual string you are putting in it's place.

append()

The append() function will take 1 argument which is a string you wish to append to the END of the string. It will not append it anywhere else. For that, you want to use the index function.

Strings & Array's

In C++, a string can be treated the same as an array with respect to its index. Here is what the string "Hello" looks like in an array format (pardon the mess).

Index  0 1 2 3 4
       H e l l o

Like arrays, the [] operator is used when accessing data. The below program uses this technique.

Example 2:
Strings as arrays

Download source code here (Right click - Save Tagret As...)

#include <iostream>
#include <string>
using namespace std;

int main(){

string name = "Alexander";

//loop to print out the string
for(int i = 0; i < name.length(); i++){
	cout << name[i];
}
cout << endl;

//loop to print out the string funny:
for(int i = 0; i < name.length(); i++){
	if(i % 2 == 0){
		cout << name[5];
	}else{
		cout << name[i];
	}
}
cout << endl;

return 0;
}

This program will output the following when complete:

Alexander
nlnxnnnen

Strings & Cin

In C++, the string library has another function called getline(). This is used when trying to get an entire line of data including white spaces, from either cin or a text file (see next tutorial).

Here is how the function looks:

getline( stream, variable )

The getline function will take 2 parameters. The first parameter, stream, will either be cin or an input file stream. The second parameter, variable, will be a string variable of an appropriate name to place the line of data.

The below program will demonstrate the use of getline().

Example 3:
getline example

Download source code here (Right click - Save Tagret As...)

#include <iostream>
#include <string>
using namespace std;

int main(){

string name = "";

cout << "Enter your full name: ";
getline(cin, name);

cout << name << endl;

return 0;
}

This program will accept the full name of a user, which will include a white space or too, and place it in the name variable.


<< Tutorial 7
Tutorial 9 >>