index string c++
To index a string in C++, you can use the []
operator. Here's an example:
#include <iostream>
#include <string>
int main() {
std::string str = "Hello, World!";
char letter = str[7];
std::cout << "The letter at index 7 is: " << letter << std::endl;
return 0;
}
In this example, the string "Hello, World!" is stored in the variable str
. The 7th character of the string is accessed using str[7]
, and it is assigned to the variable letter
. Finally, the value of letter
is printed to the console.