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 FinalState[5] = { 0,0,0,0,1 };
- void clearBuffer(void) {
- int c;
- while ((c = getchar()) != '\n' && c != EOF);
- }
- int takeUserInput() {
- int input;
- bool isCorrect = false;
- while (!isCorrect) {
- isCorrect = true;
- const int result = scanf("%d", &input);
- if (result != 1) {
- isCorrect = false;
- printf("invalid input, try again\n");
- }
- clearBuffer();
- }
- return input;
- }
- int takeUserInputFromRange(const int minValue, const int maxValue) {
- int input = 0;
- bool isCorrect = false;
- while (!isCorrect) {
- isCorrect = true;
- input = takeUserInput();
- if (input < minValue || input > maxValue) {
- isCorrect = false;
- printf("invalid input, range limited\n");
- }
- }
- return input;
- }
- enum charType getCharType(const char c) {
- if (c == '0') {
- return ctZero;
- }
- if (c > '0' && 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 isHexString(const char* string);
- void clearString(char* string, const size_t length) {
- for (int i = 0; i < length; i++) {
- string[i] = 0;
- }
- }
- void showValidSubstring(const char* string) {
- const size_t length = strlen(string);
- char *subString = malloc((length + 1) * sizeof(char));
- clearString(subString, length + 1);
- unsigned int index = 0;
- for (int i = 0; i < length; i++) {
- const enum charType charClass = getCharType(string[i]);
- if (charClass != ctError) {
- unsigned int tempState = 1;
- unsigned int j = i;
- bool isCorrect = true;
- while (isCorrect && j < length) {
- const enum charType charLocalClass = getCharType(string[j]);
- if (charLocalClass == ctError) {
- isCorrect = false;
- }
- else {
- tempState = CURRENT_STATE[tempState][charLocalClass];
- if (tempState > 0) {
- subString[index++] = string[j];
- }
- else {
- isCorrect = false;
- }
- }
- j++;
- }
- if (isHexString(subString)) {
- printf("%s is a valid substring \n", subString);
- }
- }
- clearString(subString, index);
- index = 0;
- }
- }
- bool isHexString(const char* string) {
- int currentState = 1;
- const size_t length = strlen(string);
- size_t i = 0;
- bool isHex = true;
- while (isHex && i < length) {
- const enum charType charClass = getCharType(string[i]);
- currentState = CURRENT_STATE[currentState][charClass];
- isHex = currentState;
- i++;
- }
- return isHex & FinalState[currentState - 1];
- }
- void takeCorrectFilePath(char* path, const size_t length) {
- bool isCorrect = false;
- printf("Please input the path of the correct file:\n");
- while (!isCorrect) {
- isCorrect = true;
- fgets(path, length, stdin);
- path[strcspn(path, "\n")] = 0;
- FILE* file = fopen(path, "r");
- if (file == NULL) {
- printf("File not found\n");
- isCorrect = false;
- }
- else {
- fclose(file);
- }
- }
- }
- int readIntegerFromFile(FILE* file) {
- int number;
- fscanf(file, "%d", &number);
- fgetc(file);
- return number;
- }
- bool isSpaceSymbol(const char symbol) {
- const bool result = symbol == ' ' || symbol == '\n' || symbol == '\r' || symbol == '\t';
- return result;
- }
- void Trim(char* string) {
- const size_t length = strlen(string);
- int startIndex = 0;
- int endIndex = length - 1;
- while (isSpaceSymbol(string[startIndex])) {
- startIndex++;
- }
- while (isSpaceSymbol(string[endIndex])) {
- endIndex--;
- }
- memmove(string, string + startIndex, endIndex + 1);
- string[endIndex - startIndex + 1] = 0;
- }
- void validateString(const char* string) {
- if (isHexString(string)) {
- printf("valid hex string: %s\n", string);
- }
- else {
- showValidSubstring(string);
- printf("not a valid hex string: %s\n", string);
- }
- }
- void freeStrings(char** strings, const size_t stringCount) {
- if (strings != NULL) {
- for (int i = 0; i < stringCount; i++) {
- free(strings[i]);
- }
- free(strings);
- }
- }
- char** takeStringFormFile(void) {
- char filePath[50];
- takeCorrectFilePath(filePath, 50);
- FILE* file = fopen(filePath, "r");
- const int stringCount = readIntegerFromFile(file);
- char **strings = malloc(stringCount * sizeof(char *));
- for (int i = 0; i < stringCount; i++) {
- const int maxStringLength = 25;
- strings[i] = (char *)malloc(maxStringLength * sizeof(char));
- if (fgets(strings[i], maxStringLength, file) != NULL) {
- strings[i][strcspn(strings[i], "\n")] = '\0';
- Trim(strings[i]);
- }
- else {
- clearBuffer();
- }
- }
- fclose(file);
- return strings;
- }
- char* takeStringFromConsole(const size_t length) {
- bool isCorrect = false;
- char* str = malloc(length * sizeof(char));
- while (!isCorrect) {
- isCorrect = true;
- printf("Enter a string:", length);
- fgets(str, length, stdin);
- if (strlen(str) == 1) {
- isCorrect = false;
- printf("Empty string,enter a valid string \n \n");
- }
- else if (str[strlen(str) - 1] != '\n') {
- isCorrect = false;
- printf("Too long string, enter a valid string \n \n");
- clearBuffer();
- }
- else {
- Trim(str);
- }
- }
- return str;
- }
- void fileProcess() {
- char** strings = takeStringFormFile();
- const int length = sizeof(strings) / sizeof(strings[0]);
- for (int i = 0; i < length; i++) {
- printf("%d: %s\n", i + 1, strings[i]);
- validateString(strings[i]);
- printf("\n");
- }
- freeStrings(strings, length);
- }
- void consoleProcess(const size_t size) {
- while (true) {
- char *string = takeStringFromConsole(size);
- string[strcspn(string, "\n")] = 0;
- validateString(string);
- printf("\n");
- free(string);
- }
- }
- void takeStringFromUserInput(const size_t size) {
- printf("Please enter input type: \n");
- printf("0 - console, 1 - file \n");
- const int input = takeUserInputFromRange(0, 1);
- if (input == 0) {
- consoleProcess(size);
- }
- else {
- takeStringFormFile();
- }
- }
- int main(void) {
- takeStringFromUserInput(15);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement