Welcome to The Coding College! Strings are an essential part of C++ programming, allowing you to store and manipulate sequences of characters. In this tutorial, you’ll learn how to use strings in C++, including their declaration, operations, and common methods.
What Are Strings in C++?
A string is a sequence of characters enclosed in double quotes (" "
). In C++, strings can be handled in two primary ways:
- C-Style Strings: Character arrays (
char[]
). - C++ Strings: Using the
std::string
class from the Standard Template Library (STL).
This guide focuses on std::string
, as it is more modern and versatile compared to C-style strings.
Declaring Strings in C++
Using std::string
#include <iostream>
#include <string> // Include the string library
using namespace std;
int main() {
string greeting = "Hello, World!";
cout << greeting << endl;
return 0;
}
Output:
Hello, World!
String Operations
1. Assigning Strings
You can assign values to strings using the assignment operator (=
).
string name = "Alice";
2. Concatenating Strings
Use the +
operator or the append()
method to combine strings.
string firstName = "Alice";
string lastName = "Smith";
// Using +
string fullName = firstName + " " + lastName;
cout << fullName << endl;
// Using append()
fullName = firstName.append(" ").append(lastName);
cout << fullName << endl;
Output:
Alice Smith
Alice Smith
3. Accessing Characters
You can access individual characters using the subscript operator ([]
) or the .at()
method.
Example:
string word = "Hello";
cout << word[0] << endl; // H
cout << word.at(1) << endl; // e
4. Modifying Characters
Strings are mutable, so you can change individual characters.
Example:
string word = "Hello";
word[0] = 'Y';
cout << word << endl; // Yello
5. String Length
Use the .size()
or .length()
method to get the length of a string.
Example:
string word = "Programming";
cout << "Length: " << word.length() << endl;
Output:
Length: 11
Common String Methods
Method | Description | Example |
---|---|---|
.size() | Returns the size of the string. | word.size() |
.length() | Returns the length of the string. | word.length() |
.empty() | Checks if the string is empty. | word.empty() |
.substr(pos, n) | Extracts a substring starting at pos , of length n . | word.substr(0, 4) |
.find(str) | Finds the position of str in the string. | word.find("gram") |
.replace(pos, n, str) | Replaces n characters at pos with str . | word.replace(0, 4, "Code") |
.append(str) | Appends str to the string. | word.append(" Rocks") |
Practical Examples
Example 1: String Input
Use getline()
to read an entire string, including spaces.
#include <iostream>
#include <string>
using namespace std;
int main() {
string name;
cout << "Enter your name: ";
getline(cin, name); // Reads the entire line
cout << "Hello, " << name << "!" << endl;
return 0;
}
Output:
Enter your name: John Doe
Hello, John Doe!
Example 2: String Manipulation
Extract and modify substrings.
#include <iostream>
#include <string>
using namespace std;
int main() {
string sentence = "Welcome to The Coding College!";
// Extract substring
string word = sentence.substr(11, 3); // "The"
cout << "Substring: " << word << endl;
// Replace a word
sentence.replace(11, 3, "Your");
cout << "Modified Sentence: " << sentence << endl;
return 0;
}
Output:
Substring: The
Modified Sentence: Welcome to Your Coding College!
Using Strings in Loops
You can iterate through strings using loops to access or manipulate characters.
Example:
#include <iostream>
#include <string>
using namespace std;
int main() {
string word = "Code";
for (int i = 0; i < word.size(); i++) {
cout << word[i] << endl;
}
return 0;
}
Output:
C
o
d
e
Difference Between std::string
and C-Style Strings
Feature | C-Style Strings | std::string |
---|---|---|
Ease of Use | Less user-friendly | More user-friendly |
Memory Management | Manual | Automatic |
Built-in Functions | Limited | Rich set of methods |
Flexibility | Fixed size | Dynamic size |
Explore More at The Coding College
This guide introduces the basics of strings in C++. To dive deeper into topics like advanced string manipulation, regular expressions, or memory optimization, explore our C++ tutorials.
What’s Next?
- Learn about C++ arrays to handle collections of data.
- Master file handling to work with text stored in external files.