#include #include #include int main(int argc, char ** argv) { char buf[32]; int len = 4, c, n = 0; while ((c = getopt(argc, argv, "p:l:")) != -1) { switch (c) { case 'p': if (optarg[0] == '1') len = 4; else if (optarg[0] == '2') len = 14; else { fprintf(stderr, "Unexpected part %s\n", optarg); return -1; } break; case 'l': len = atoi(optarg); if (len < 2 || len > 32) { fprintf(stderr, "Unexpected marker length: %d\n", len); return -1; } break; default: return -1; } } while ((c = fgetc(stdin)) != EOF && c >= 'a' && c <= 'z') { unsigned int t = 0; int i; buf[n % len] = c; ++n; if (n < len) continue; // All input chars are between 'a' and 'z'. // Check for repeats with a bit field. for (i = 0; i < len; ++i) { unsigned int b = 1u << (buf[i] - 'a'); if (t & b) break; t |= b; } if (i == len) // No repeats break; } if (c == EOF) { printf("No marker read in %d bytes\n", n); return 1; } if (c < 'a' || c > 'z') { printf("Unexpected input after %d bytes\n", n); return 1; } printf("Marker of length %d read after %d bytes\n", len, n); return 0; }