Sarah said:
I want to get all numbers ;-( in the following ip. eliminate all
periods for me plzzzzzzzzzz :-( :-(
142.42.75.67
The best way is to use the standard template library (STL). Learn it
well and use it often!
#include <string>
#include <algorithm>
#include <iostream>
int main(int /*argc*/, char** /*argv*/)
{
std::string s("142.42.75.67");
std::cout << "Now the string s is: " << s << std::endl;
std::replace(s.begin(), s.end(), '.', ' ');
std::cout << "Replaced periods with space and now the string s is:
"
<< s << std::endl;
s.erase(std::remove(s.begin(), s.end(), ' '), s.end());
std::cout << "Finely the string s is: " << s << std::endl;
return 0;
}
The way std::remove works can be a little confusing. It do not remove
anythig but move the selected value, a character in this case, to the
end of the sequence between the first iterator(s.begin()) and the
second iterator (s.end()), and then return an iterator pointing at the
first of the removed characters. The member function erase erases
everything from the first iterator sent to the function(here the return
value from std::remove), to the, but not including, the last iterator
sent to the function.
I will point out that the stl algorithms can be a little har to grasp,
but when you understand one of them, most of them works the same easy
way, and they will make you a better programmer
-Thomas Gulbrandsen