Convert String to Char in C++
Get a single character out of a C++ string with [], at(), or c_str(). Learn which one checks the index, what at() throws, and why an empty string needs care.
1#include <iostream>2#include <string>3using namespace std;45int main() {6 string str = "Hello";78 // Method 1: Using index operator9 char ch1 = str[0];1011 // Method 2: Using at() method12 char ch2 = str.at(0);1314 // Method 3: Using c_str()15 const char* cstr = str.c_str();16 char ch3 = cstr[0];1718 cout << "String: " << str << endl;19 cout << "Character (method 1): " << ch1 << endl;20 cout << "Character (method 2): " << ch2 << endl;21 cout << "Character (method 3): " << ch3 << endl;2223 return 0;24}
Sample Output
Character (method 1): H
str[0].
Character (method 3): H
First char through c_str().
In this one
- How str[0] reads the first character
- How str.at(0) checks the index and throws out_of_range
- What c_str() returns and how long the pointer stays valid
- How to handle an empty string
How it works
- 1
str holds "Hello".
- 2
str[0] gives 'H' with no bounds check.
- 3
str.at(0) gives 'H' and would throw if 0 were invalid.
- 4
c_str() returns a const char* to the same characters; cstr[0] is 'H'.
- 5
Print the results.
Edge cases
Empty string
Set str = "".
str.at(0) throws std::out_of_range. str[0] on an empty string returns the terminating '\0', but any larger index is undefined behavior.
Pointer from c_str after a change
Call str += "!" after getting cstr.
Modifying the string can move its storage, so cstr may now point to freed memory. Call c_str() again after changes.
What's going on
The program extracts a single character from a string. Since a string is a sequence of characters, you can access individual characters by their position (index). The code shows multiple ways to get the first character 'H' from the string "Hello".
Example:
Header Files Used
Declaring Variables
The program declares:
string str = "Hello";
Method 1: Using Index Operator []
char ch1 = str[0];
This is the simplest and most common method:
How it works:
Advantages:
Important Note:
Method 2: Using at() Method
char ch2 = str.at(0);
This method provides bounds checking:
How it works:
Advantages:
Example:
try {
char ch = str.at(0); // Safe
char ch2 = str.at(10); // Throws exception
} catch (const std::out_of_range& e) {
// Handle error
}
Method 3: Using c_str()
const char* cstr = str.c_str();
char ch3 = cstr[0];
This method converts the string to a C-style string:
How it works:
Advantages:
Important Notes:
Other Methods (Mentioned but not shown in code)
Method 4: Using front()
char ch4 = str.front();
Method 5: Using Iterators
char ch5 = *str.begin();
Displaying Results
The program prints:
cout << "String: " << str << endl;
cout << "Character (method 1): " << ch1 << endl;
cout << "Character (method 2): " << ch2 << endl;
cout << "Character (method 3): " << ch3 << endl;
Output:
String: Hello
Character (method 1): H
Character (method 2): H
Character (method 3): H
All three methods produce the same character 'H'.
When to Use Each Method
Accessing Other Characters
You can access any character in the string:
Example:
string s = "Hello";
char first = s[0]; // 'H'
char last = s[4]; // 'o'
char middle = s[2]; // 'l'
Important Safety Considerations
Bounds Checking:
Best Practice: Use at() when the index might be invalid, use [] when you're certain it's valid.
Empty Strings:
return 0;
This ends the program successfully.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
What does string("Hi").at(5) do?
Pick an answer to check it.
Question 2
What type does c_str() return?
Pick an answer to check it.
Questions people ask
[] or at()?
Use at() when the index might be wrong and you want an error. Use [] in tight loops where you already know the index is valid.
How do I get the last character?
str.back() or str[str.size() - 1], after checking the string is not empty.
Can I change a character?
Yes: str[0] = 'J'; turns Hello into Jello. You cannot write through c_str().