From e57f0309a4085ec9b965d85609dad27959d3f98e Mon Sep 17 00:00:00 2001 From: Eric Bagus Date: Thu, 8 Feb 2024 17:56:30 +0100 Subject: [PATCH] Added operation handler formatting check --- src/operationHandler.c | 26 ++++++++++++++++++++++++++ src/operationHandler.h | 2 ++ 2 files changed, 28 insertions(+) diff --git a/src/operationHandler.c b/src/operationHandler.c index 0f63fb7..ef41efb 100644 --- a/src/operationHandler.c +++ b/src/operationHandler.c @@ -1,5 +1,7 @@ #include "operationHandler.h" #include +#include +#include bool checkOperationInput(int input) { switch (input) { @@ -7,4 +9,28 @@ bool checkOperationInput(int input) { return true; } return false; +} + +bool containsTwoNumbers(const char* str) { + int numbersCount = 0; + bool hasSpace = false; + bool isInNumber = false; + + for (int i = 0; str[i] != '\0'; i++) { + if (isdigit(str[i])) { + if (!isInNumber) { + isInNumber = true; + numbersCount++; + } + } else if (str[i] == ' ' && isInNumber) { + hasSpace = true; + isInNumber = false; + } else if (str[i] == ' ' && !isInNumber) { + hasSpace = false; + } else { + hasSpace = false; + isInNumber = false; + } + } + return (numbersCount == 2 && hasSpace); } \ No newline at end of file diff --git a/src/operationHandler.h b/src/operationHandler.h index 5293b1e..cb083e2 100644 --- a/src/operationHandler.h +++ b/src/operationHandler.h @@ -4,4 +4,6 @@ bool checkOperationInput(int); +bool containsTwoNumbers(const char*); + #endif //THEADMIRALS_OPERATIONHANDLER_H