Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <assert.h>
- #include <inttypes.h>
- #include <ctype.h>
- static bool is_ip(std::string &s)
- {
- size_t sl = s.size();
- assert (sl <= 20);
- size_t cnt = 0;
- for (size_t i = 0; i < sl; i++) {
- char c = s[i];
- if (c == '.') {
- cnt++;
- continue;
- }
- assert (isdigit(c));
- assert (uint32_t(c - '0') < 10);
- }
- assert (cnt == 3);
- s += '.';
- std::string t;
- for (size_t i = 0; i <= sl; i++) {
- char c = s[i];
- if (c != '.') {
- t += c;
- continue;
- }
- size_t tl = t.size();
- if (tl == 0)
- return false;
- unsigned int x = 0;
- for (size_t i = 0; i < tl; i++) {
- unsigned int v = t[i] - '0';
- x = x * 10 + v;
- if (x > 255)
- return false;
- }
- t.clear();
- }
- return true;
- }
- int main()
- {
- std::string s;
- std::cin >> s;
- assert (std::cin);
- bool ok = is_ip(s);
- std::cout << (ok ? "Good" : "Bad") << std::endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement