Strings and characters

In Python, the character string (str) is one of the immutable data types. It means that you cannot change a string-type value:

def main():
    text = "abcdefg"
    print(text[3])  # Prints d
    text[3] = "X"   # Error!
    print(text)

main()

The problem was avoided by creating a new string-type value every time they wanted to edit the text of the string. The new, edited text was inserted to the new string, and the string was named after the original string:

def main():
    text = "abcdefg"
    print(text[3])  # Prints d
    text = text[:3] + "X" + text[4:]
    print(text)     # Prints abcXefg

main()

In C++, you can change an existing string (except if it is declared as constant, i.e. const string):

#include <iostream>
#include <string>   // Note: string is a library type in C++

using namespace std;

int main() {
     string text = "abcdefg";
     cout << text.at(3) << endl; // Prints d
     text.at(3) = 'X';           // Note: char type literal 'X'
     cout << text << endl;       // Prints abcXefg
}

Both Python and C++ start indexing the characters in a string at zero.

In C++, the single characters of a string are of the char type, which means that the indexing operation text.at(indeksi) will always produce a char type value, or if the target of the assignment is the indexing operation itself, the value assigned must be char.

Operations that target strings are called methods in C++, just like in Python:

#include <iostream>
#include <string>

using namespace std;

int main() {
     string text = "One string to rule them all...";
     cout << text.length() << endl;
     cout << text.substr(4, 14) << endl;
     text.replace(4, 6, "thing");
     cout << text << endl;
}

Some operations that target strings will return numerical values, such as:

text.length();     // Number of characters
text.find("abc");  // At which point can you find the first "abc"?

If these numerical values connected to strings (e.g. the amount of characters, indexes) are to be saved in a variable, the type of the variable must be string::size_type:

#include <iostream>
#include <string>

using namespace std;

int main() {
     string name = "";
     string::size_type temp = 0;

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

     temp = name.length();
     cout << "Your name has " << temp << " characters" << endl;

     temp = name.find("nen");
     if ( temp == string::npos ) {
          cout << "Your name doesn't contain \"nen\"." << endl;
     } else {
          cout << "Combination \"nen\" is located starting from "
               << temp << endl;
     }
}

Please note that find returns the value string::npos if it cannot find the combination of characters it was looking for.

Some useful string operations

Methods

Methods are called with the notation variable.method(parameters).

  • string::size_type length()

    The return value tells how many characters there are in a string.

    string::size_type len = 0;
    len = text.length();
    
  • char at(index)

    Returns the index-th character of the string. If calling the at function is the object of an assignment, it replaces index-th character.

    char letter;
    letter = text.at(5);
    text.at(2) = 'k';  // Please note the type char
    
  • string erase(index)

    string erase(index, len)

    If there is only one parameter, it will destroy all the characters in the string, starting at index. If the call also has the parameter len (length), it will destroy as many characters onward as the parameter len describes.

    text.erase(4);
    text.erase(2, 5);
    
  • string::size_type find(target)

    string::size_type find(target, index)

    Starting at the beginning of the string (or at the point index), the above function will find the first occurrence of the string target, and return the index in question. If target cannot be found, the return value is the constant string::npos.

    string::size_type location = 0;
    location = text.find("abc", 5);
    if ( location == string::npos ) {
        // Could not find "abc" from index 5 onwards
    } else {
        // "abc" was found.
    }
    
  • string::size_type rfind(target)

    string::size_type rfind(target, index)

    This one is similar to the function find earlier, but this will start the search at the end of the string and proceed towards the beginning.

  • string substr(index)

    string substr(index, len)

    Returns a part of the string. If you only have one parameter, the return value will be the substring that starts at index and finishes at the end of the string. If you also give the parameter len (length), the function will return as many characters as len describes onwards from index.

    string text = "abcdefg";
    string result;
    result = text.substr(3);     // "defg"
    result = text.substr(4, 2);  // "ef"
    
  • string insert(index, addition)

    Adds the text addition before position index.

    string text = "abcd";
    text.insert(2, "xy");  // "abxycd"
    
  • string replace(index, len, replacement)

    Replaces the contents of the string with the text replacement, starting at index for the length of len characters.

    string text = "ABCDEF";
    text.replace(1, 3, "xy");  // "AxyEF"
    

Operators

  • text1 == text2

    text1 != text2

    text1 < text2

    text1 > text2

    text1 <= text2

    text1 >= text2

    You can compare different strings to each other with relational operators. The operators that include a ”smaller than” or ”larger than” compare the so-called lexical order, which for strings means something close to their alphabetical order.

  • text1 + text2

    Glues (concatenates) two strings into one.

    string test1 = "Qt";
    string test2 = "Creator";
    string result;
    result = test1 + " " + test2;  // "Qt Creator"
    
  • str += addition

    Glues (concatenates) an addition to the end of the string. The addition may be either a string or a single character (char type value).

    string result = "Qt";
    result += ' ';        // "Qt "
    result += "Creator";  // "Qt Creator"
    

Functions

  • getline(stream, line)

    Reads one line of text from a file or the keypad (cin) and save it in the string-type reference parameter line.

  • int stoi(text)

    Changes the parameter text to the corresponding integer.

    string numeric_text = "123";
    int number;
    number = stoi(numeric_text);  // 123
    
  • double stod(text)

    Just like stoi above, but this changes the string into a real number.

    string numeric_text = "123.456";
    double number;
    number = stod(numeric_text);  // 123.456
    

The problem with the functions stoi and stod is that they do not recognise a faulty input if the beginning just looks like a value of the right type. For example, the stoi function accepts "123abc" and returns 123. With the knowledge you have gathered so far, it is not easy to spot this mistake.

Characters

In Python, we processed characters (letters) the same way we do a one character string.

In C++, the type char is actually an integer. A char variable includes an ASCII value:

cout << "Enter a character: ";
char ch = ' ';
cin >> ch;
int ascii_value_of_ch = static_cast< int >( ch );
cout << "ASCII value of " << ch << " is " << ascii_value_of_ch << endl;

The fact that the character is actually an integer enables you to perform calculations on the char type:

for( char letter = 'a'; letter < 'z'; ++letter ){
    cout << letter;
}
cout << endl;

This might be useful if you, for some reason, wish your program to go through all the letters in alphabetical order.

Library cctype

In C++, you can use the library called cctype (don’t forget include), which contains the following functions, among others:

  • islower(character) checks if you have a lowercase letter (return value bool)
  • isupper(character) checks if you have an uppercase letter
  • isdigit(character) checks if you have a digit
  • tolower(character) converts an uppercase letter into a corresponding lowercase letter
  • toupper(character) converts a lowercase letter into a corresponding uppercase letter.

You will find more functions included in the cctype library on the Internet in the many C++ library references there are.