Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2016
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.02 KB | None | 0 0
  1. //Write a program to Validate an IPv4 Address.
  2. #include<iostream>
  3. #include<string>
  4. using namespace std;
  5.  
  6. bool isDigit(char ch)
  7. {
  8. return ((ch>='0') && (ch<='9'))?true:false;
  9. }
  10.  
  11. bool function(string str)
  12. {
  13. int temp = 0, dots = 0, i = 0;
  14. int len = str.length();
  15. while(i<len)
  16. {
  17. if(!i && str[i] == '.')
  18. return false;
  19. else if(i>0 && str[i] == '.' && str[i-1] == '.')
  20. return false;
  21. else if(str[i] == '.')
  22. {
  23. dots++;
  24. i++;
  25. if((dots>3) || (i==len))
  26. return false;
  27. temp = 0;
  28. continue;
  29. }
  30. else if(str[i] != '.' && !isDigit(str[i]))
  31. return false;
  32. temp = temp*10 + str[i]-'0';
  33. if(temp>255 || dots>3)
  34. return false;
  35. i++;
  36. if(i==len && dots<3)
  37. return false;
  38. }
  39. }
  40.  
  41. int main()
  42. {
  43. string str;
  44. cout<<" Enter string :- ";
  45. cin>>str;
  46. cout<<(function(str)?" Valid ":" Invalid ")<<endl;
  47. system("PAUSE");
  48. return 0;
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement