C++ Strings
Strings are objects that represent sequences of characters. In C++, there are two ways to create strings:
- Using the
stringclass from the Standard Template Library (STL) - Using C-style character arrays
string Class
The standard string class provides robust support for string operations in C++. To use this class, include the <string> header:
#include <string>std::string greeting = "Hello, World!";std::string empty_string = "";std::string user_name = "@developer123";std::string message = "Good morning!";
Creating and Initializing Strings
A string can be created and initialized in several ways:
#include <iostream>#include <string>int main() {// Different ways to create stringsstd::string s1; // Empty stringstd::string s2 = "C++"; // Initialization with string literalstd::string s3("Programming"); // Using constructorstd::string s4(s2); // Copy of another stringstd::string s5(5, 'a'); // String with 5 'a' characters: "aaaaa"std::cout << "s1: " << s1 << std::endl;std::cout << "s2: " << s2 << std::endl;std::cout << "s3: " << s3 << std::endl;std::cout << "s4: " << s4 << std::endl;std::cout << "s5: " << s5 << std::endl;return 0;}
Accessing and Modifying Strings
Individual characters inside a string can be accessed and modified using indices, just like arrays:
#include <iostream>#include <string>int main() {std::string text = "Hello";// Accessing characterschar first = text[0]; // 'H'char last = text[4]; // 'o'// Modifying characterstext[0] = 'J'; // Changes "Hello" to "Jello"std::cout << "Modified string: " << text << std::endl;return 0;}
Basic String Operations
The string class provides several useful methods for string manipulation:
#include <iostream>#include <string>int main() {std::string text = "C++ Programming";// String length/sizestd::cout << "Length: " << text.length() << std::endl; // 15std::cout << "Size: " << text.size() << std::endl; // 15 (same as length)// Check if emptystd::cout << "Is empty: " << text.empty() << std::endl; // 0 (false)// Substring extractionstd::string sub = text.substr(0, 3); // "C++"std::cout << "Substring: " << sub << std::endl;// Find position of a substringsize_t position = text.find("Programming");if (position != std::string::npos) {std::cout << "Found at position: " << position << std::endl; // 4}return 0;}
String Concatenation
Strings can be joined with the + operator or the .append() method:
#include <iostream>#include <string>int main() {std::string first_name = "Ada";std::string last_name = "Lovelace";// Using + operatorstd::string full_name = first_name + " " + last_name;// Using .append() methodstd::string greeting = "Hello, ";greeting.append(first_name);// Using += operatorstd::string message = "Welcome ";message += "to C++!";std::cout << full_name << std::endl;std::cout << greeting << std::endl;std::cout << message << std::endl;return 0;}
This example results in:
Ada LovelaceHello, AdaWelcome to C++!
C-Style Character Strings
C-style strings are character arrays that originated from the C language and continue to be supported in C++. In C, a string represents an array of characters, terminated by a null character '\0'.
char message[] = "Howdy";
The memory representation of this C-style string looks like this:
Character | 'H' 'o' 'w' 'd' 'y' '\0'Index | 0 1 2 3 4 5Address | 23451 23452 23453 23454 23455 23456
Creating C-Style Strings
C-style strings can be created in several ways:
#include <iostream>int main() {// Different ways to create C-style stringschar str1[] = "C++"; // Automatic size calculationchar str2[4] = "C++"; // Explicitly specify size (including null character)char str3[] = {'C', '+', '+', '\0'}; // Character by character with null terminatorchar str4[100] = "Programming"; // With extra space allocationstd::cout << "str1: " << str1 << std::endl;std::cout << "str2: " << str2 << std::endl;std::cout << "str3: " << str3 << std::endl;std::cout << "str4: " << str4 << std::endl;return 0;}
Working with C-Style Strings
C-style strings require the <cstring> header for string manipulation operations:
#include <iostream>#include <cstring>int main() {char str1[20] = "Hello";char str2[20] = "World";char result[40];// String lengthstd::cout << "Length of str1: " << strlen(str1) << std::endl;// String copystrcpy(result, str1);std::cout << "After strcpy: " << result << std::endl;// String concatenationstrcat(result, " ");strcat(result, str2);std::cout << "Concatenated string: " << result << std::endl;// String comparisonint comparison = strcmp(str1, str2);if (comparison < 0) {std::cout << "str1 is less than str2" << std::endl;} else if (comparison > 0) {std::cout << "str1 is greater than str2" << std::endl;} else {std::cout << "str1 equals str2" << std::endl;}return 0;}
The output looks like this:
Length of str1: 5After strcpy: HelloConcatenated string: Hello Worldstr1 is less than str2
string Class vs. C-Style Strings
Here are some key differences between the two string types:
| Feature | string Class |
C-Style Strings |
|---|---|---|
| Memory management | Automatic | Manual |
| Bounds checking | Yes | No |
| Size | Dynamic (can grow/shrink) | Fixed at declaration |
| Null termination | Handled automatically | Must be managed manually |
| Concatenation | Using + operator |
Using strcat() |
| Comparison | Using ==, !=, etc. |
Using strcmp() |
| Character access | Using [] or at() |
Using [] |
| Required header | <string> |
<cstring> |
| Memory safety | Safe | Prone to buffer overflows |
Note: The
stringclass is generally preferred in modern C++ for its safety and convenience features.
Converting Between String Types
You can convert between std::string and C-style strings:
#include <iostream>#include <string>#include <cstring>int main() {// C-style string to std::stringchar cstr[] = "Hello C++";std::string str1(cstr);// std::string to C-style stringstd::string str2 = "C++ Programming";const char* cstr2 = str2.c_str(); // Get a C-style string pointerstd::cout << "str1: " << str1 << std::endl;std::cout << "cstr2: " << cstr2 << std::endl;return 0;}
Frequently Asked Questions
1. What’s the difference between length() and size() for strings?
In C++, the length() and size() methods of the string class are functionally identical. Both return the number of characters in the string. The dual naming is historical - size() is consistent with other STL containers, while length() is more intuitive for strings.
std::string text = "Hello";std::cout << text.length(); // Returns 5std::cout << text.size(); // Also returns 5
2. Why should I use string class instead of C-style strings?
The string class provides several advantages over C-style strings:
- Memory management: Automatically handles allocation and deallocation
- Safety: Protects against buffer overflows
- Functionality: Provides built-in methods for common operations
- Flexibility: Can dynamically grow and shrink as needed
C-style strings require manual memory management and are prone to errors like buffer overflows.
3. How do I convert a number to a string in C++?
The simplest way to convert a number to a string in modern C++ is to use the std::to_string() function:
For example: std::to_string(42) returns the string "42".
For converting a string to a number, you can use functions like std::stoi() (string to int), std::stof() (string to float), etc. For example, std::stoi("42") returns the integer 42.
Strings
- .append()
- Appends characters or strings to the end of an existing string.
- .assign()
- Assigns new content to a string, replacing its current contents with specified characters, substrings, or values.
- .capacity()
- Returns the number of characters a string can contain before allocating new memory.
- .compare()
- Compares two strings lexicographically and returns an integer indicating their relative order.
- .c_str()
- Returns a pointer to a null-terminated character array that represents the contents of the string.
- .erase()
- Removes characters from a C++ string object.
- .find()
- Returns the index of the first occurrence of the specified string or character.
- .front()
- Returns a reference to the first character in a string.
- .insert()
- Inserts characters or strings at a specified position within an existing string.
- .length()
- Returns the length of the string it is called on.
- .replace()
- Returns a string with a portion replaced by another string.
- .size()
- Returns the size of the string it is called on.
- .substr()
- Extracts a substring from a given string, creating a new string object.
- .swap()
- Exchanges the contents of two strings.
- rfind()
- Searches a string from right to left and returns the last occurrence of a substring or character.
- strcmp()
- Compares two strings and returns an integer value.
- strcpy()
- Copies one string into another and returns the newly copied string.
- strtok()
- Breaks a C-style string into a series of tokens using a list of delimiters.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn C++ on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours
- Learn C++ — a versatile programming language that’s important for developing software, games, databases, and more.
- Beginner Friendly.11 hours