#include#include int main() { std::string str = "Hello World!"; std::cout << "Length of string: " << str.length() << std::endl; return 0; }
#includeThis example initializes a string and loops through each character using an index variable and the length function. The output is "I a m a s t r i n g .".#include int main() { std::string str = "I am a string."; int len = str.length(); for (int i = 0; i < len; i++) { std::cout << str[i] << " "; } return 0; }