codepad
[
create a new paste
]
login
|
about
Language:
C
C++
D
Haskell
Lua
OCaml
PHP
Perl
Plain Text
Python
Ruby
Scheme
Tcl
#include <string> #include <sstream> #include <iostream> using namespace std; struct sIP{ int a; int b; int c; int d; }; int main(){ sIP pIP = {0}; int * ptr = (int *)&pIP; stringstream ss( "127.0.0.0\n" "127.0.0,1\n" "192.168.0.01\n" "-50.0.2.15\n" "1.2.3\n" "255.00.255.255\n"); string line; size_t size; char delim; while( getline(ss, line) ) { stringstream s(line); cout<<"CHECK FOR : "<<line<<" : "; for( size = 0; size < 4; size++ ) { if( !(s>>ptr[size]) ) break; if( size < 3 ) if(!(s>>delim) ) break; if( delim != '.' ) break; if( ptr[size] < 0 ) break; if( ptr[size] > 255 ) break; } if( size != 4 ) cout<<"INVALID IP STRING"<<endl; else cout<<"CORRECT IP STRING"<<endl; } return 0; }
Private
[
?
]
Run code
Submit