get an IP address

S

Sarah Russinovich

I want to get all numbers ;-( in the following ip. eliminate all
periods for me plzzzzzzzzzz :-( :-(
142.42.75.67

thanks
<blah,bleh,blih>

-Sarah Russinovic
 
B

benben

Sarah said:
I want to get all numbers ;-( in the following ip. eliminate all
periods for me plzzzzzzzzzz :-( :-(
142.42.75.67

thanks
<blah,bleh,blih>

-Sarah Russinovic

Sounds like homework.
 
F

Frederick Gotham

Sarah Russinovich posted:
I want to get all numbers ;-( in the following ip. eliminate all
periods for me plzzzzzzzzzz :-( :-(
142.42.75.67


What you have is an array of char's as follows:

char str[13] = {'1','4','2','.','4','2','.','7','5','.','6','7',0};

One possible route would be to set a pointer to the first char, and
increment it until you reach a fullstop. If you replace the fullstop with a
null character, then you can use it as a string. Something like:

#include <cstdio>

void GetNumbers(char *str,int (*DecimalStr)(char const*))
{
for(char *p = str; ; ++p)
{
switch(*p)
{
case '.':
{
*p = 0;
DecimalStr(str);
str = p + 1;
break;
}

case 0:
{
DecimalStr(str);
return;
}
}
}
}

int main()
{
char str[] = "142.42.75.67";

GetNumbers(str,std::puts);
}

If you would like to convert each individual string to an integer type,
then get yourself a C++ (or C) Standard Library reference, and have a look
at what functionality it provides.
 
T

Thomas

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top