判断一个字符串是否ip地址(包含ipv4和ipv6)

[复制链接]
 楼主| keer_zu 发表于 2023-9-15 11:04 | 显示全部楼层 |阅读模式



代码如下:
  1. #include <iostream>
  2. #include <algorithm>
  3. using namespace std;

  4. class Solution
  5. {
  6. public:
  7.     string validIPAddress(string IP)
  8.     {
  9.         //以.和:来区分ipv4和ipv6
  10.         for (int i = 0; i < IP.length(); i++)
  11.         {
  12.             if (IP[i] == '.')
  13.                 return isIPv4(IP) ? "IPv4" : "Neither";
  14.             else if (IP[i] == ':')
  15.                 return isIPv6(IP) ? "IPv6" : "Neither";
  16.         }
  17.         return "Neither";
  18.     }
  19. private:
  20.     bool isIPv4(string IP)
  21.     {
  22.         int dotcnt = 0;
  23.         //数一共有几个.
  24.         for (int i = 0; i < IP.length(); i++)
  25.         {
  26.             if (IP[i] == '.')
  27.                 dotcnt++;
  28.         }
  29.         //ipv4地址一定有3个点
  30.         if (dotcnt != 3)
  31.             return false;
  32.         string temp = "";
  33.         for (int i = 0; i < IP.length(); i++)
  34.         {
  35.             if (IP[i] != '.')
  36.                 temp += IP[i];
  37.             //被.分割的每部分一定是数字0-255的数字
  38.             if (IP[i] == '.' || i == IP.length() - 1)
  39.             {
  40.                 if (temp.length() == 0 || temp.length() > 3)
  41.                     return false;
  42.                 for (int j = 0; j < temp.length(); j++)
  43.                 {
  44.                     if (!isdigit(temp[j]))
  45.                         return false;
  46.                 }
  47.                 int tempInt = stoi(temp);
  48.                 if (tempInt > 255 || tempInt < 0)
  49.                     return false;
  50.                 string convertString = to_string(tempInt);
  51.                 if (convertString != temp)
  52.                     return false;
  53.                 temp = "";
  54.             }
  55.         }
  56.         if (IP[IP.length()-1] == '.')
  57.             return false;
  58.         return true;
  59.     }
  60.     bool isIPv6(string IP) {
  61.         int dotcnt = 0;
  62.         for (int i = 0; i < IP.length(); i++)
  63.         {
  64.             if(IP[i] == ':')
  65.                 dotcnt++;
  66.         }
  67.         if (dotcnt != 7) return false;
  68.         string temp = "";
  69.         for (int i = 0; i < IP.length(); i++)
  70.         {
  71.             if (IP[i] != ':')
  72.                 temp += IP[i];
  73.             if (IP[i] == ':' || i == IP.length() - 1)
  74.             {
  75.                 if (temp.length() == 0 || temp.length() > 4)
  76.                     return false;
  77.                 for (int j = 0; j < temp.length(); j++)
  78.                 {
  79.                     if (!(isdigit(temp[j]) ||(temp[j] >= 'a' && temp[j] <= 'f') || (temp[j] >= 'A' && temp[j] <= 'F')))
  80.                         return false;
  81.                 }
  82.                 temp = "";
  83.             }
  84.         }
  85.         if (IP[IP.length()-1] == ':')
  86.             return false;
  87.         return true;
  88.     }
  89. };

  90. int main()
  91. {
  92.     Solution sol;
  93.     string s;
  94.     while(1){
  95.         cout << "Please input a string: " << endl;
  96.             cin >> s;
  97.         if(s == "exit"){
  98.                 break;
  99.         }
  100.             cout << sol.validIPAddress(s) << endl;
  101.     }
  102.     cout << "bye" << endl;
  103.     return 0;
  104. }


运行结果:


您需要登录后才可以回帖 登录 | 注册

本版积分规则

个人签名:qq群:49734243 Email:zukeqiang@gmail.com

1478

主题

12917

帖子

55

粉丝
快速回复 在线客服 返回列表 返回顶部
个人签名:qq群:49734243 Email:zukeqiang@gmail.com

1478

主题

12917

帖子

55

粉丝
快速回复 在线客服 返回列表 返回顶部