Goal: To understand how to initialize and manipulate text strings.

Individual variable types can be combined and be used as one unit. This unit is known as a container. A string is a type of container as it is a chain of characters. Words, and full sentences are examples of strings. This lesson will focus on using the string container offered in the <string> library.

In order to use standard strings from the string library, an include statement must be made at the beginning of the C++ file. The string library includes not only the container itself, but member functions that are specifically for that container. Member functions can perform many operations including string manipulation. A detailed list of the string library can be found here.

Variable Initialization and Input

Once the string library has been included, a string can be initialized like any other variable. When a string is initialized, the string itself will always start off empty. Therefore, outputting the string immediately after initializing will simply output an empty string ("") and not garbage.

When std::cin from the <iostream> library is used to get a value for a string, the console will only accept all characters until it reaches whitespace. This means cin cannot be used to store an input that is a sentence with spaces. If inputs are supposed to have spaces, another function is used. getline is a function that is used to get an entire line of input and store it into a string. It is formatted like the following:

<span id="L1" class="line"><span class="n">getline</span><span class="p">(</span><span class="n">cin</span><span class="p">,</span> <span class="n">variableName</span><span class="p">);</span></span>
Example Code:
<span id="L1" class="line"><span class="cp">#include &lt;iostream&gt;</span></span>
<span id="L2" class="line"><span class="cp">#include &lt;string&gt;</span></span>
<span id="L3" class="line"><span class="k">using</span> <span class="k">namespace</span> <span class="n">std</span><span class="p">;</span></span>
<span id="L4" class="line"><span class="kt">int</span> <span class="nf">main</span><span class="p">()</span> <span class="p">{</span></span>
<span id="L5" class="line">    <span class="n">string</span> <span class="n">name</span><span class="p">;</span>       <span class="c1">// initializes string</span></span>
<span id="L6" class="line">    <span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">"What's your name?"</span> <span class="o">&lt;&lt;</span> <span class="n">endl</span><span class="p">;</span></span>
<span id="L7" class="line">    <span class="n">getline</span><span class="p">(</span><span class="n">cin</span><span class="p">,</span><span class="n">name</span><span class="p">);</span> <span class="c1">// stores full name</span></span>
<span id="L8" class="line">    <span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">"Hi "</span> <span class="o">&lt;&lt;</span> <span class="n">name</span> <span class="o">&lt;&lt;</span> <span class="s">"!"</span><span class="p">;</span></span>
<span id="L9" class="line">    <span class="k">return</span> <span class="mi">0</span><span class="p">;</span></span>
<span id="L10" class="line"><span class="p">}</span></span>
Sample Run:
>>> What's your name?
Herp da Derp
>>> Hi Herp da Derp!
Note
If cin was used before getline, cin.ignore() should be placed before getline(). cin.ignore() is a function that clears any inputs that are not stored in a variable and is required for getline() to work properly.

Using Member Functions and String Manipulation

As for all libraries for C++, a list of all member functions can be found online. For a string, every function is made to manipulate/extract information from a string. Therefore, most of the functions in the string are formatted as follows:

<span id="L1" class="line"><span class="n">stringName</span><span class="p">.</span><span class="n">memberFunction</span><span class="p">(</span><span class="n">parameters</span><span class="p">)</span> <span class="c1">// notice the dot</span></span>

For example, to find the length of a string, strName.length() will return an integer that represents the length of the string with the name, strName.

A string can be assigned text surrounded by quotation marks with an equal sign (=).

String concatenation is the appending of two or more strings. Strings can be added with text or other strings using the + operator. Since the + operator can be used, the += operator can be used as well.

Example Code:
<span id="L1" class="line"><span class="cp">#include &lt;iostream&gt;</span></span>
<span id="L2" class="line"><span class="cp">#include &lt;string&gt;</span></span>
<span id="L3" class="line"></span>
<span id="L4" class="line"><span class="k">using</span> <span class="k">namespace</span> <span class="n">std</span><span class="p">;</span></span>
<span id="L5" class="line"><span class="kt">int</span> <span class="nf">main</span><span class="p">()</span> <span class="p">{</span></span>
<span id="L6" class="line">    <span class="n">string</span> <span class="n">str1</span> <span class="o">=</span> <span class="s">"abc"</span><span class="p">,</span> <span class="n">str2</span> <span class="o">=</span> <span class="s">"efg"</span><span class="p">,</span> <span class="n">str3</span><span class="p">;</span> <span class="c1">// initialized 3 strings</span></span>
<span id="L7" class="line">    <span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">"str3 is "</span> <span class="o">&lt;&lt;</span> <span class="n">str3</span><span class="p">.</span><span class="n">length</span><span class="p">()</span> <span class="o">&lt;&lt;</span> <span class="s">" characters long."</span> <span class="o">&lt;&lt;</span> <span class="n">endl</span><span class="p">;</span></span>
<span id="L8" class="line">    <span class="n">str3</span> <span class="o">=</span> <span class="n">str2</span><span class="p">;</span>          <span class="c1">// assigns a value to str3</span></span>
<span id="L9" class="line">    <span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">"str3 is "</span> <span class="o">&lt;&lt;</span> <span class="n">str3</span><span class="p">.</span><span class="n">length</span><span class="p">()</span> <span class="o">&lt;&lt;</span> <span class="s">" characters long."</span> <span class="o">&lt;&lt;</span> <span class="n">endl</span><span class="p">;</span></span>
<span id="L10" class="line">    <span class="n">str3</span> <span class="o">+=</span> <span class="s">"d"</span> <span class="o">+</span> <span class="n">str1</span><span class="p">;</span>     <span class="c1">// concatenates str3 with "d" and str1</span></span>
<span id="L11" class="line">    <span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">"str3 is "</span> <span class="o">&lt;&lt;</span> <span class="n">str3</span><span class="p">.</span><span class="n">length</span><span class="p">()</span> <span class="o">&lt;&lt;</span> <span class="s">" characters long."</span> <span class="o">&lt;&lt;</span> <span class="n">endl</span><span class="p">;</span></span>
<span id="L12" class="line">    <span class="k">return</span> <span class="mi">0</span><span class="p">;</span></span>
<span id="L13" class="line"><span class="p">}</span></span>
Output:
str3 is 0 characters long.
str3 is 3 characters long.
str3 is 7 characters long.

The plus symbol (+) is the only arithmetic operator that can be used with a string. Do not try and subtract strings using the - operator. Likewise, = and += are the only assignment operators that can be used for strings.

Note
Do not add two string literals (text surrounded by quotation marks) together.
<span id="L1" class="line"><span class="n">str</span> <span class="o">+=</span> <span class="s">"abc"</span> <span class="o">+</span> <span class="s">"def"</span><span class="p">;</span> <span class="c1">// "abc" can't add with "def"</span></span>

The above code will result in an error because the computer is trying to add two string literals. This could be fixed by altering the code like:

<span id="L1" class="line"><span class="n">str</span> <span class="o">=</span> <span class="p">(</span><span class="n">str</span> <span class="o">+</span> <span class="s">"abc"</span><span class="p">)</span> <span class="o">+</span> <span class="s">"def"</span><span class="p">;</span> <span class="c1">// str adds with "abc", then with "def"</span></span>

That way, a string will always add with a string literal and two string literals will never add with each other. The reason why strings are able to add with string literals are beyond the scope of this course. Just make sure to not add string literals together.

Character Manipulation

Since a string is simply a sequence of characters, each character acts as its own variable and can be accessed and manipulated individually. Accessing a specific character in a string is formatted as follows, str[n] where the character returned will be the nth character in the string.

image

Each character in a string has an index. All the characters in a string are numbered in order starting with index 0. For example, str[1] will give the second character in a string. Indexes are typically used with for loops in order to access every character in a string.

Note
Be Careful! str.length() returns the length of a string, but the highest index in that string will always be one less than the length of the string.
<span id="L1" class="line"><span class="cp">#include &lt;iostream&gt;</span></span>
<span id="L2" class="line"><span class="cp">#include &lt;string&gt;</span></span>
<span id="L3" class="line"><span class="c1">// This code outputs the reverse of a word</span></span>
<span id="L4" class="line"><span class="k">using</span> <span class="k">namespace</span> <span class="n">std</span><span class="p">;</span></span>
<span id="L5" class="line"><span class="kt">int</span> <span class="nf">main</span><span class="p">()</span> <span class="p">{</span></span>
<span id="L6" class="line">    <span class="n">string</span> <span class="n">str</span><span class="p">;</span>             <span class="c1">// initializes string</span></span>
<span id="L7" class="line">    <span class="n">cin</span> <span class="o">&gt;&gt;</span> <span class="n">str</span><span class="p">;</span>             <span class="c1">// gets input (no sentences)</span></span>
<span id="L8" class="line">    <span class="n">string</span> <span class="n">reverseStr</span><span class="p">;</span>      <span class="c1">// string to store the reverse of str</span></span>
<span id="L9" class="line">    <span class="c1">// i is initialized as the highest index of str</span></span>
<span id="L10" class="line">    <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="n">str</span><span class="p">.</span><span class="n">length</span><span class="p">()</span> <span class="o">-</span> <span class="mi">1</span><span class="p">;</span> <span class="n">i</span> <span class="o">&gt;=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">i</span><span class="o">--</span><span class="p">)</span> <span class="p">{</span> <span class="c1">// index decreases by 1 each loop</span></span>
<span id="L11" class="line">        <span class="c1">// in this context, i acts as an index number</span></span>
<span id="L12" class="line">        <span class="n">reverseStr</span> <span class="o">+=</span> <span class="n">str</span><span class="p">[</span><span class="n">i</span><span class="p">];</span> <span class="c1">// string is made by repeated concatenation</span></span>
<span id="L13" class="line">    <span class="p">}</span></span>
<span id="L14" class="line">    <span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="n">str</span> <span class="o">&lt;&lt;</span> <span class="s">" backwards is "</span> <span class="o">&lt;&lt;</span> <span class="n">reverseStr</span><span class="p">;</span> <span class="c1">// outputs reverse str</span></span>
<span id="L15" class="line">    <span class="k">return</span> <span class="mi">0</span><span class="p">;</span></span>
<span id="L16" class="line"><span class="p">}</span></span>
Sample Run:
Kappa
>>> Kappa backwards is appaK

ASCII

ASCII stands for American Standard Code for Information Interchange. ASCII ensures that every character is paired with a numerical value. Each character in a string is stored as that numerical value and every character can be referred to be either the character itself, or its numerical representation.

ASCII is further explained in Chapter 7.

For a list of all characters and their numerical counterparts, click here.

In Summary

  • Containers are used to organize and store various variable types at once

  • A string is a type of container that stores a sequence of characters

  • Standard strings are found in the <string> library

  • Commands from a library are called member functions

  • getline() is used to get inputs that includes spaces

  • Member functions are formatted as str.memberFunction(parameters)

  • Strings can be concatenated with other strings or characters.

  • Each individual character can be accessed with str[]

  • Index refers to where a character is in a string

  • The first character in a string is always index 0.

  • ASCII is used to pair every character with a numerical value.

Contest Questions