Monday, December 13, 2010

Strings 101 - Part III

Obtaining the length of a string. Using the length() string method.

This tutorial is about processing character strings using C++. Specifically, it's about using the Standard Template Library (STL) string library.

The STL is a library of classes, algorithms and iterators provided as part of a complete C++ installation that simplify programming tasks. The STL provides templates for vectors, algorithms for searching and sorting, and iterators to help traverse lists and sequences. The STL is an extremely valuable library containing some of the most useful data structures and algorithms in computer science.

In this tutorial, we will concentrate on only one of them, the string library.

As you saw in the last section, to use the string library in C++ you need to reference it using the following code:

#include <string>  

We also included the <iostream> library in our code. That library allowed us to use the cout function to write output to the console.

The STL string library (or class) contains many useful functions and we will explore the following ones:

lengthdetermines the length of the string
insertinsert a string inside another one
eraseremove characters from a string
replacereplace a portion of a string with another string
findsearch for a portion of a string
substrextract a portion of a string
+=append to the string
comparecompare two strings

Let's start with a simple example. This program prompts the user from some input, echoes the input that the user typed in, as well as the number of characters in the input.

To obtain input from the user, we'll use the iostream function cin and it's member function getline. Here's the full program. Type it in and save it as s2.cpp.

File s2.cpp
#include <iostream>
#include <string>
using namespace std;

int main()
{
    char buffer[256];
    string s;

    cout << "Type in some text: ";
    cin.getline(buffer, 256);

    s = buffer;

    cout << "You typed in: " << s < < endl;
    cout << "There are " << s.length() << " characters in your text" << endl;

    return 0;
}

Analysis:

The first three lines should be familiar.

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

These add the <iostream> and <string> libraries to our program. We also specify that we are in the std namespaces which means that functions are assumed to be in the std namespace unless we specifically state that they aren't. This is a little confusing so we'll say more about namespaces later.

The next part of the program is the entry point:

int main()

Every C++ program has a main() function.

The next two statements declare two variables that we're going to use:

char buffer[256];
string s;

The char buffer[256] declaration requests a character array that's 256 characters long. We're going to use this to collect the input that the user types in. The function that we'll use cin.getline() expects to put it's input in a character array, so we've declared one for this purpose. We specify that the maximum number of characters that this array can hold is 256 characters.

The next declaration, string s, is our string variable, or object.

The next two lines request the user to type something, and then collect what's typed in:

cout << "Type in some text: ";
cin.getline(buffer, 254);

The next line copies the text that's typed in from the character buffer[] array where it was stored, to our string object.

s = buffer;

Finally, we'll display what was typed in and also the number of characters that were typed in.

cout << "\n\nYou typed in: " << s << endl;
cout << "There are " << s.length() << " characters in your text" << endl;

The interesting function is:

s.length()

The function, or in object-oriented lingo, method, length() returns the length of the object.

In this case, the statement s.length() effectively returns the length of the string, s.

Finally, a return statement to indicate that everything was successful. Remember that returning the value of zero indicates no errors, success.

No comments:

Post a Comment