Lesson 4
Mastering String Manipulation in C++
Lesson Overview

Welcome to an engaging C++ session! Today, we will delve deeper into handling string data in C++. Consider the situations in which you have to analyze text data, like constructing a web scraper or developing a text-based algorithm to interpret the user reviews of a website. All these cases require an efficient handling of strings, which involves analyzing and manipulating them. In this lesson, we will focus on how to traverse strings and perform operations on each character using C++.

The objective of this lesson is to become proficient in using C++ loops with a specific emphasis on strings. We will explore the techniques of string indexing and practice character operations using C++ functions.

Working with ASCII Codes in Characters

Characters in C++ can be manipulated using their ASCII values. ASCII (American Standard Code for Information Interchange) is a character encoding standard used to represent text in computers and other devices that use text. Every character has a unique ASCII value.

You can convert a character into its ASCII value using a simple cast:

C++
1#include <iostream> 2 3int main() { 4 char c = 'A'; 5 int ascii_val = static_cast<int>(c); 6 std::cout << "The ASCII value of " << c << " is: " << ascii_val << std::endl; 7 return 0; 8}

Similarly, you can convert an ASCII value back to its corresponding character:

C++
1#include <iostream> 2 3int main() { 4 int ascii_val = 65; 5 char c = static_cast<char>(ascii_val); 6 std::cout << "The character of ASCII value " << ascii_val << " is: " << c << std::endl; 7 return 0; 8}

Manipulating the ASCII value of characters can be quite useful in certain situations. For example, to convert a lowercase letter to uppercase (or vice versa), you could subtract (or add) 32 to the character's ASCII value.

String Indexing Reminder

C++ strings work with a zero-based indexing system. This means that you can access specific characters in a string by using their position.

Please note: If you try to access an index that does not exist in your string, C++ will read random memory, which could lead to unpredictable results. Hence, it is recommended always to check the string length before accessing any index.

Here's an example:

C++
1#include <iostream> 2#include <string> 3 4int main() { 5 std::string text = "Hello, C++!"; 6 if (text.length() >= 10) { 7 char tenth_char = text[9]; 8 std::cout << "The tenth character is: " << tenth_char << std::endl; 9 } else { 10 std::cout << "The string is too short!" << std::endl; 11 } 12 return 0; 13}
Character Operations

Let's now explore the character operations in C++. We have functions like toupper(), tolower(), and type-checking functions that you can use to perform operations on characters. Here are some examples:

  • The toupper() and tolower() functions are useful when comparing strings irrespective of their case.
C++
1#include <iostream> 2#include <string> 3#include <cctype> 4 5int main() { 6 std::string s = "mark"; 7 for (char& c : s) c = std::toupper(c); 8 std::cout << s << std::endl; // Prints: 'MARK' 9 10 s = "Mark"; 11 for (char& c : s) c = std::tolower(c); 12 std::cout << s << std::endl; // Prints: 'mark' 13 14 return 0; 15}
  • Functions islower() and isupper() can be used to determine whether a character is a lowercase or uppercase letter.
C++
1#include <iostream> 2#include <cctype> 3 4int main() { 5 char a = 'a'; 6 char b = 'B'; 7 std::cout << std::boolalpha; // print bool as true/false 8 9 std::cout << "Is " << a << " lowercase? " << static_cast<bool>(islower(a)) << std::endl; // Prints: true 10 std::cout << "Is " << b << " lowercase? " << static_cast<bool>(islower(b)) << std::endl; // Prints: false 11 12 std::cout << "Is " << a << " uppercase? " << static_cast<bool>(isupper(a)) << std::endl; // Prints: false 13 std::cout << "Is " << b << " uppercase? " << static_cast<bool>(isupper(b)) << std::endl; // Prints: true 14 15 return 0; 16}
  • The isalpha(), isdigit(), and isalnum() functions are useful when you need to check whether the character satisfies a specific condition (is a letter, a digit, or a letter/digit).
C++
1#include <iostream> 2#include <cctype> 3 4int main() { 5 std::cout << std::boolalpha; // print bool as true/false 6 7 std::cout << static_cast<bool>(std::isalpha('C')) << std::endl; // Prints: true 8 std::cout << static_cast<bool>(std::isalpha('+')) << std::endl; // Prints: false 9 10 std::cout << static_cast<bool>(std::isdigit('9')) << std::endl; // Prints: true 11 std::cout << static_cast<bool>(std::isdigit('D')) << std::endl; // Prints: false 12 13 std::cout << static_cast<bool>(std::isalnum('6')) << std::endl; // Prints: true 14 std::cout << static_cast<bool>(std::isalnum('k')) << std::endl; // Prints: true 15 std::cout << static_cast<bool>(std::isalnum('?')) << std::endl; // Prints: false 16 17 return 0; 18}
Lesson Summary and Practice

Excellent work! We have learned how to work with strings in C++ by looping over them, managing string indices, and manipulating characters using functions in C++. Moreover, we also have explored strategies to manage any unpredictable results while dealing with strings in our programs.

Real-world problems abound where string operations can be handy. From designing smart typewriters and web scrapers to crafting AI bots, mastering string operation is a valuable skill in the world of programming. Therefore, don't waste any time! Jump into the practice problems to reinforce your learning. Your journey is just beginning – see you in the upcoming sessions!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.