Lesson 3
Navigating the World of C++ Strings
Introduction to Strings in C++

Welcome back to our C++ course! Today, we'll unravel the data structure known as Strings. Strings are sequences of characters functioning like vectors, thus accommodating elements, enabling access to them at certain positions, removing elements, and much more. Whenever you handle text data in your programs, strings are your go-to tool. Let's dive in!

Suppose you wish to store "Happy Birthday Alice!" on an invitation card in a program. This text can be stored as a string. Like a vector holding characters, a string specializes in holding text, highlighting the significance of textual information in programming.

Declaring a String

To declare a string is analogous to declaring an integer: you use the string keyword, followed by a string variable name. Let's declare a msg string for the invitation message!

C++
1#include <iostream> 2#include <string> // Includes the string library to work with strings 3 4int main() { 5 std::string msg; 6 std::cout << "The invitation message: " << msg << std::endl; // Prints "The invitation message:" 7 return 0; 8}

We declared the string msg, which is currently empty, resulting in the output "The invitation message:".

Strings in C++ must be surrounded by double quotes. Let's also declare and initialize a non-empty string:

C++
1#include <iostream> 2#include <string> // Includes the string library to work with strings 3 4int main() { 5 std::string greeting = "Hello, World!"; 6 std::cout << greeting << std::endl; // Prints "Hello, World!" 7 return 0; 8}
Assigning a Value

We'll extend msg, crafting our party invitation using the assignment operator (=).

C++
1#include <iostream> 2#include <string> 3 4int main() { 5 std::string msg; 6 msg = "Happy Birthday Alice!"; 7 std::cout << "The invitation message: " << msg << std::endl; // Prints "The invitation message: Happy Birthday Alice!" 8 return 0; 9}

We've now populated msg to welcome Alice!

Adding and Accessing Elements in a String

Fetching specific string elements is akin to vector elements, facilitated by the index within square brackets [].

C++
1#include <iostream> 2#include <string> 3 4int main() { 5 std::string msg = "Happy Birthday Alice!"; 6 std::cout << "The first character of the string: " << msg[0] << std::endl; // Prints "The first character of the string: H" 7 return 0; 8}

This time, we've retrieved the first character 'H' from the invitation.

Modifying Characters in a String

Did you accidentally invite "Alics"? Not a problem! Strings can modify characters just like vectors. Remember, the new element must be of type char so it must be surrounded by single quotes (').

C++
1#include <iostream> 2#include <string> 3 4int main() { 5 std::string msg = "Happy Birthday Alics!"; 6 msg[19] = 'e'; // Corrects the typo 7 std::cout << "The invitation message: " << msg << std::endl; // Prints "The invitation message: Happy Birthday Alice!" 8 return 0; 9}

Here, we've rectified the typo by changing the 20th character from 's' to 'e'.

Accessing the String's Length

Strings in C++ boast handy functions similar to vectors. One of them is size, which is very helpful to learn the string's length. It returns the number of characters in the string.

Here's how you can utilize it:

C++
1#include <iostream> 2#include <string> 3 4int main() { 5 std::string msg = "Happy Birthday Alice!"; 6 // Number of characters in msg: 21 7 std::cout << "Number of characters in msg: " << msg.size() << std::endl; 8 return 0; 9}

Here, we find out the number of letters in the msg string using the .size() method. It works pretty much the same as the vector's analogous method.

Exploring String Concatenation

We can use the + operator to append one string to another, combining them into a single new string. It is called concatenation. Here's how you can concatenate strings:

C++
1#include <iostream> 2#include <string> 3 4int main() { 5 std::string part1 = "Happy Birthday"; 6 std::string part2 = " Alice!"; 7 std::string msg = part1 + part2; // Concatenates "Happy Birthday" and " Alice!" into "Happy Birthday Alice!" 8 std::cout << "The invitation message: " << msg << std::endl; // Prints "The invitation message: Happy Birthday Alice!" 9 return 0; 10}

In this example, we start with two separate strings part1 and part2. By using the + operator, we concatenate them into a single string msg, resulting in the complete invitation message.

Lesson Summary

Congratulations on learning about strings in C++! Now, you are equipped with the knowledge to declare strings, add and extract elements, modify characters, and understand string functions. Get ready for practice exercises designed to reinforce your understanding and confidence with strings! Are you ready? Let's go!

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