Actually if a valid name can contain both an apostrophe (
') and dash (
-) at the same time, then my idea isn't the correct solution.
One way to correct it could be to count apostrophes and dashes independently, not in the same
total variable.
| ianol wrote: |
|---|
| can you explain your for loop more |
It would help me if you could say what you need me to explain.
Anyway, I will assume that you wonder about iterators, and so I'll talk about those.
Normally in a
for() loop, when you want to go over all elements of an array in order ("iterate") you use indexes. This is the natural, C way of doing things.
However the C++ style of iterating containers is by using their given iterators.
So:
1 2 3 4 5 6 7
|
for (std::string::const_iterator ci = name.begin(); ci != name.end(); ++ci)
std::cout << *ci << '\n';
// kind of equivalent to...
for (std::size_t i=0; i < name.length(); ++i)
std::cout << name[i] << '\n';
|
I know that from the example above, iterating doesn't make a lot of sense, but be aware that there are containers which cannot access their elements by an index, for example:
std::list,
std::set,
std::map. So it becomes a reflex to avoid using indexes.
http://www.cplusplus.com/reference/stl/