What would be a good way to determine if a string contains an IPv4 address? Should I use isdigit()?
I'll give the "don't want two problems" solution:
#include
int isIp_v4( char* ip){
int num;
int flag = 1;
int counter=0;
char* p = strtok(ip,".");
while (p && flag ){
num = atoi(p);
if (num>=0 && num<=255 && (counter++<4)){
flag=1;
p=strtok(NULL,".");
}
else{
flag=0;
break;
}
}
return flag && (counter==3);
}
EDIT: strtok may not be thread safe (credits to Adam Rosenfield)