Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <ctype.h>
- #include <stdbool.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- enum CharType {
- ctError, ctDollar, ctZero, ctXLetter, ctDigit, ctLetter
- };
- const int CURRENT_STATE[6][6] = {
- {0, 0, 0, 0, 0, 0},
- {0, 2, 3, 0, 0, 0},
- {0, 0, 5, 0, 5, 5},
- {0, 0, 0, 4, 0, 0},
- {0, 0, 5, 0, 5, 5},
- {0, 0, 5, 0, 5, 5}
- };
- const int FINAL_STATE[5] = { 0, 0, 0, 0, 1 };
- void clearInputBuffer() {
- while (getchar() != '\n');
- }
- int getInputChoice() {
- int input;
- while (scanf("%d", &input) != 1) {
- clearInputBuffer();
- printf("Invalid input, please try again:\n");
- }
- clearInputBuffer();
- return input;
- }
- enum CharType identifyCharType(char c) {
- if (c == '0') return ctZero;
- if (c >= '1' && c <= '9') return ctDigit;
- if ((c >= 'A' && c <= 'F') || (c >= 'a' && c <= 'f')) return ctLetter;
- if (c == 'x' || c == 'X') return ctXLetter;
- if (c == '$') return ctDollar;
- return ctError;
- }
- bool checkHexString(const char* str) {
- int state = 1;
- while (*str) {
- state = CURRENT_STATE[state][identifyCharType(*str++)];
- if (state == 0) return false;
- }
- return FINAL_STATE[state - 1];
- }
- void validateHexString(const char* str) {
- if (checkHexString(str)) {
- printf("Valid hex string: %s\n", str);
- }
- else {
- printf("Not a valid hex string: %s\n", str);
- }
- }
- void removeWhitespace(char* str) {
- char* end = str + strlen(str) - 1;
- while (end > str && isspace((unsigned char)*end)) end--;
- *(end + 1) = '\0';
- while (*str && isspace((unsigned char)*str)) str++;
- }
- char* readStringFromConsole(size_t length) {
- char* str = malloc(length);
- printf("Enter a string: ");
- fgets(str, length, stdin);
- removeWhitespace(str);
- return str;
- }
- void extractHexSubstrings(const char* str) {
- size_t length = strlen(str);
- printf("Finding valid hex substrings:\n");
- for (size_t start = 0; start < length; start++) {
- for (size_t end = start + 1; end <= length; end++) {
- char* substring = malloc(end - start + 1);
- strncpy(substring, str + start, end - start);
- substring[end - start] = '\0';
- if (checkHexString(substring)) {
- printf("Valid hex substring: %s\n", substring);
- }
- free(substring);
- }
- }
- }
- void handleFileInput() {
- char filePath[50];
- printf("Enter the file path:\n");
- fgets(filePath, sizeof(filePath), stdin);
- removeWhitespace(filePath);
- FILE* file = fopen(filePath, "r");
- if (!file) {
- printf("File not found.\n");
- return;
- }
- int stringCount;
- fscanf(file, "%d", &stringCount);
- fgetc(file);
- for (int i = 0; i < stringCount; i++) {
- char str[100];
- if (fgets(str, sizeof(str), file)) {
- removeWhitespace(str);
- validateHexString(str);
- extractHexSubstrings(str);
- }
- }
- fclose(file);
- }
- void handleUserInput() {
- printf("Choose input method (0 - console, 1 - file): ");
- int choice = getInputChoice();
- if (choice == 0) {
- char* str = readStringFromConsole(100);
- validateHexString(str);
- extractHexSubstrings(str);
- free(str);
- }
- else {
- handleFileInput();
- }
- }
- int main() {
- handleUserInput();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement