← Back to C++ Course | Chapter 6: Arrays & Strings | Lesson 12 of 15

C++ Accessing String Characters

Individual characters in a std::string can be accessed with square-bracket indexing or the at() method, and looping through those indices visits every character in order.
Syntax
cpp
variable_name[index];
variable_name.at(index);  // with bounds checking

Accessing Characters with []

Square-bracket indexing, the same syntax used for arrays, retrieves the character at a specific zero-based position in a std::string.

Example: Accessing Characters with []

cpp
// Include std::cout and std::cin
#include <iostream>
// Include std::string
#include <string>

// Program execution starts in main()
int main() {
	std::string name = "Alex";
	// Print to the console with cout
	std::cout << name[0] << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Accessing Characters with at()

The at() method retrieves a character the same way [] does, but performs bounds checking and throws an out_of_range exception for an invalid index instead of causing undefined behavior.

Example: Accessing Characters with at()

cpp
#include <iostream>
#include <string>

int main() {
	std::string name = "Alex";
	std::cout << name.at(0) << std::endl;
	// name.at(10); // throws std::out_of_range
	return 0;
}

Modifying a Character

A specific character in a mutable std::string can be changed by assigning a new value through [] or at(), replacing just that one position.

Example: Modifying a Character

cpp
// Include std::cout and std::cin
#include <iostream>
// Include std::string
#include <string>

// Program execution starts in main()
int main() {
	std::string name = "Alex";
	name[0] = 'a';
	// Print to the console with cout
	std::cout << name << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

The First and Last Characters

front() and back() are convenient shortcuts for retrieving the first and last characters of a string, equivalent to str[0] and str[str.length() - 1] but clearer to read.

Example: The First and Last Characters

cpp
// Include std::cout and std::cin
#include <iostream>
// Include std::string
#include <string>

// Program execution starts in main()
int main() {
	std::string name = "Alex";
	// Print to the console with cout
	std::cout << name.front() << " " << name.back() << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Looping Through Every Character

A for loop with an index from 0 to length() - 1, or a range-based for loop, is the standard way to visit and process every character in a std::string in order.

Example: Looping Through Every Character

cpp
// Include std::cout and std::cin
#include <iostream>
// Include std::string
#include <string>

// Program execution starts in main()
int main() {
	std::string word = "Hi!";
	for (char c : word) {
		// Print to the console with cout
		std::cout << c << " ";
	}
	// Print to the console with cout
	std::cout << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Using s[10] on a short string, which is not bounds checked and is undefined behavior, instead of s.at(10), which throws std::out_of_range.
  2. Calling s.front() or s[0] on an empty string, which is undefined behavior.
  3. Using s[s.length()] as the last character, when the last valid index is s.length() - 1.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.