C++ Is Ipv 4 Adress
#include <algorithm>
#include <cctype>
#include <string>
#include <vector>
bool isIPv4Address(const std::string& inputString)
{
std::vector<std::string> parts;
std::string cur;
for (char c : inputString) {
if (c == '.') {
parts.push_back(cur);
cur.clear();
} else {
cur += c;
}
}
parts.push_back(cur);
for (const auto& v : parts) {
if (v.empty() || !std::all_of(v.begin(), v.end(), [](unsigned char c) { return std::isdigit(c); })) {
return false;
}
if (v.size() > 1 && v[0] == '0') {
return false; // rejects leading zeros, mirrors $v !== (string)(int)$v
}
if (std::stol(v) > 255) {
return false;
}
}
return parts.size() == 4;
}
This splits the string by dots and validates each part as a normal IPv4 octet.