Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Write a program to Validate an IPv4 Address.
- #include<iostream>
- #include<string>
- using namespace std;
- bool isDigit(char ch)
- {
- return ((ch>='0') && (ch<='9'))?true:false;
- }
- bool function(string str)
- {
- int temp = 0, dots = 0, i = 0;
- int len = str.length();
- while(i<len)
- {
- if(!i && str[i] == '.')
- return false;
- else if(i>0 && str[i] == '.' && str[i-1] == '.')
- return false;
- else if(str[i] == '.')
- {
- dots++;
- i++;
- if((dots>3) || (i==len))
- return false;
- temp = 0;
- continue;
- }
- else if(str[i] != '.' && !isDigit(str[i]))
- return false;
- temp = temp*10 + str[i]-'0';
- if(temp>255 || dots>3)
- return false;
- i++;
- if(i==len && dots<3)
- return false;
- }
- }
- int main()
- {
- string str;
- cout<<" Enter string :- ";
- cin>>str;
- cout<<(function(str)?" Valid ":" Invalid ")<<endl;
- system("PAUSE");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement