As Albatross noted you can check if the current character is a digit with the isdigit library function.
To iterate through the string in an effective way is somewhat more tricky...
A good idea is an infinite loop (
while(true)
or
for(;;)
), inside which you use the find_if function.
(take a look at this:
http://cplusplus.com/reference/algorithm/find_if/ )
A string::iterator would be useful here. Declare one and initialize it to the beginning of your string (
str.begin()
). Then use find_if with your iterator,
str.end()
and
your IsDigit function. This is important, you can't use the library's isdigit directly coz its prototype doesn't match what find_if expects. Just make a wrapper of the library's function that returns a
bool. Here is an example:
1 2 3 4
|
bool IsDigit(char c)
{
return isdigit(c);
}
|
If find_if returns
str.end()
we are done... It means that no digits were found. In this case use
break
to stop the loop. Else it will return an iterator pointing at the place of the digit. Use this info and the string reference Albatross provided to see how you can do the replacement you want.