Basics
Recall from tutorial 1 the progam which accepted a name from the user
and displayed a message. Here is that program again:
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...)
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).
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...)
This program will output the following when complete:
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...)
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.