iterating string in cpp 1

iterating string in cpp

std::string s("Hello world");

for (char & c : s)
{
    std::cout << "One character: " << c << "\n";
    c = '*';
}

Here is what the above code is Doing:
1. It creates a string object s with the value "Hello world".
2. It creates a reference to a character c.
3. It loops over all the characters in s.
4. It prints the current character.
5. It replaces the current character with an asterisk.

Similar Posts