get an IP address

Discussion in 'C++' started by Sarah Russinovich, Jul 29, 2006.

  1. 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
    Sarah Russinovich, Jul 29, 2006
    #1
    1. Advertising

  2. Sarah Russinovich

    benben Guest

    Sarah Russinovich wrote:
    > 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.
    benben, Jul 29, 2006
    #2
    1. Advertising

  3. Sarah Russinovich

    Kai-Uwe Bux Guest

    Sarah Russinovich wrote:

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


    Here you go:

    142 42 75 67


    > thanks


    You'r welcome.


    Best

    Kai-Uwe Bux
    Kai-Uwe Bux, Jul 29, 2006
    #3
  4. Sarah Russinovich

    Roal Zanazzi Guest

    Kai-Uwe Bux ha scritto:
    > Sarah Russinovich wrote:
    >
    >> I want to get all numbers ;-( in the following ip. eliminate all
    >> periods for me plzzzzzzzzzz :-( :-(
    >> 142.42.75.67

    >
    > Here you go:
    >
    > 142 42 75 67
    >
    > Kai-Uwe Bux
    >

    ROTFL!!!
    Thank you, now my day is getting better :)

    --
    Roal Zanazzi
    Roal Zanazzi, Jul 29, 2006
    #4
  5. 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.

    --

    Frederick Gotham
    Frederick Gotham, Jul 29, 2006
    #5
  6. Sarah Russinovich

    Thomas Guest

    Sarah Russinovich wrote:
    > 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
    Thomas, Jul 29, 2006
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. avnrao
    Replies:
    1
    Views:
    7,218
    avnrao
    May 4, 2004
  2. Patrice
    Replies:
    0
    Views:
    881
    Patrice
    May 4, 2004
  3. Raterus
    Replies:
    0
    Views:
    432
    Raterus
    Jun 2, 2004
  4. Alex Stevens
    Replies:
    0
    Views:
    775
    Alex Stevens
    Aug 10, 2004
  5. =?Utf-8?B?QW5nZWw=?=

    Pass a Web Address and return a File System Address

    =?Utf-8?B?QW5nZWw=?=, Sep 20, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    532
    =?Utf-8?B?U3RldmUgQy4gT3Jy?=
    Sep 21, 2005
Loading...

Share This Page