Thursday, August 27, 2009

Character string type string

Since words consist of sequences or strings of characters, there is naturally also the problem of storing and processing strings in C++. One of the solutions to the problem in C++ is the string type. string is part of the C++ standard library. When it is used #include <string> must be added at the beginning of the code file. After that string behaves in the same way as a basic data type:
  • Defining variables
  • Parameters and return types of functions
  • Assignment and initialization from another string variable or literal with =
  • Printing with cout and reading input with cin
  • Comparison with ==, !=, <=, >=, <>
In addition, string has other useful properties. Let's take a closer look at the string type.

A string variable is defined:

string variable_name = "init_value";

Any text inside the quotes can be assigned to a string variable. string is a special type that does not necessarily need the "init_value" but it is usually initialized for consistency.

Reading a string:

int main( void )
{
string name = "";
cout << "What's your name?" << endl;
cin >> name;
cout << "Hello" << name << endl;
}

The output of the program:

What's your name?
Harry Potter
Hello Harry

So cin stops reading the input when it encounters the first white space character. The entire line can be read by using the function getline():

int main( void )
{
string name = "";
cout << "What's your name?" << endl;
getline( cin, name );
cout << "Hello" << name << endl;
}

Now the output will be:

What's your name?
Harry Potter
Hello Harry Potter

getline always reads the entire line (including white spaces). It has two arguments, first the input stream (cin) and second which string you wish to write to.

A string variable can contain as many characters as needed. string has a lot of functions of its own that can be used to get information about the string. The size of the string can be obtained with the length() function. The function returns the amount of characters in the string.

cout << name.length();

would print 12 after the getline. The individual characters in a string can be accessed either with the at() function or with [].

name.at( 1 ) = name[ 10 ];

There are several functions for finding substrings:

name.find( "ot" );
name.find( "ot", 3 );

find() functions return the position of the substring "ot" in the string name. If the substring is not found, the function returns a special value string::npos

A part of a stirng can be accessed with the substr() function. On the other hand replace() function lets you to write to a substring.

name.substr( 0, 5 ); //equal Harry
name.replace( 0, 5, "Ron" ); //equals Ron Potter

A part of a string can be removed with erase()

name.erase( 0, 4); // leaves just potter

append() adds a substring to the end of the string:

name.append( " the wizard" );

The length of the string changes if needed. The size can also be changed explicitly with the resize() function:

name.resize( 6 ); //leaves again just potter


Reference: cplusplus, anaturb, cppreference


Previous post

Next post




No comments:

Post a Comment