How Can I Insert Zeros in the Middle of a String of IP? C/C++

I wrote a piece of code that does the following:

1. Get IP from user in this format xx.xx.xx.xx
2. Fill zeros at the left of each segment if user did not enter 3 digits on the set, e.g.
0xx.00x.xxx.0xx

Input is: 22.3.120.32
Output should be: 022.003.120.032

Here is my code, it works but not perfectly. When user inserts 111.33.66.3, nothing happens, it works when the first segment is not 3 numbers, there are many other flaws too.

Here is my code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
#include<string>
using namespace std;
 
const int MAX_IP_SIZE = 15;
const int MAX_IP_DIGIT = 3;
 
int main ()
{
        string IP;
        int numOfZerosToFill = 0;
        int tempDigitCount = 0;
        int IPCurrentPointer = 0;
 
        cout<<"ENTER IP: ";
        cin>>IP;
 
        while (IPCurrentPointer < (int)IP.length())
        {
                char dot = IP.at(IPCurrentPointer);
             
                if(dot == '.')
                {
                        numOfZerosToFill = MAX_IP_DIGIT - tempDigitCount;
                      
                        if( numOfZerosToFill == 1 || numOfZerosToFill == 2)
                        {
                                int spotForFillingZeros = IPCurrentPointer - tempDigitCount;
                                IP.insert(spotForFillingZeros, numOfZerosToFill, '0');
                                IPCurrentPointer += numOfZerosToFill;
                               
                                tempDigitCount = 0;
                        }
                }
                else
                        tempDigitCount++;
 
                IPCurrentPointer++;
        }
 
        numOfZerosToFill = MAX_IP_DIGIT - tempDigitCount;
        if( numOfZerosToFill == 1 || numOfZerosToFill == 2)
        {
                int spotForFillingZeros = IPCurrentPointer - tempDigitCount;
                IP.insert(spotForFillingZeros, numOfZerosToFill, '0');
                IPCurrentPointer += numOfZerosToFill;
        }
 
        cout<<IP<<"******"<<endl;
        system("pause");
        return 0;
}


Do you have a better algorithm? Please note that i will receive the IP from a parameter ready & then pass it to a function that inserts zeros on the front if possible. The piece of code above is a sample out of a bigger source code.

I need help asap..plzzz

Thanks
i will use sptintf()....Thanks anyway for visiting
You can use boost::regex to split the input into the 4 octets, then use boost::format to add leading zeroes.

Or use scanf to with the format string "%u.%u.%u.%u", read into four unsigneds, then use printf with the format string
"%03u.%03u.%03u.%03u" to output with the leading zeroes.

Or you can use string.find() to find the positions of the dots and insert zeroes so that each dot is exactly 4 positions away from the previous.
I would either:
a) use regular expressions (quite powerful; )
or
b) - split the string at the dots
- process and assemble the parts

If you're interested in regular expressions, maybe this link helps:
http://www.onlamp.com/pub/a/onlamp/2006/04/06/boostregex.html?page=all

You're more flexible (and less error prone) using regular expressions and since this is not time critical why not choose a simple solution?
On a completely different note, you do realise that some systems (like Windows) will interpret such numbers as octal rather than decimal. Right?
I think splitting the string from dots is easiest
Yet another idea:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <sstream>
#include <iomanip>
//...
string ip( "172.168.0.1" );
istringstream source( ip );
ostringstream target;
for( int i = 0; i < 4; ++i )
{
    string part;
    getline( source, part, '.' );
    target << setfill( '0' ) << setw( 3 ) << part;
    if( i != 4 ) target << '.';
}
// target.str() is now 172.168.000.001 
seymore15074 (354):

Best answer A+...

Thanks to all who helped! Your answer helped in someway.
Topic archived. No new replies allowed.