From 06bb8994d4675495e3fbd660378ec370337c884d Mon Sep 17 00:00:00 2001 From: Daniel Santos Date: Fri, 11 Mar 2022 23:26:07 +0000 Subject: Check if number belongs to numerical system * add is_valid_number() * check if number belongs to the 'from' numerical system Signed-off-by: Daniel Santos --- numericx.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'numericx.c') diff --git a/numericx.c b/numericx.c index 44cd648..d12fc87 100644 --- a/numericx.c +++ b/numericx.c @@ -177,6 +177,22 @@ reverse_string(char* string) } } +bool is_valid_number(char* numeral_system, char *number) +{ + /* + if( *number == '-' || *number == '+' ) + ++number; + */ + while( !(*number == '\0') ) + { + if( strchr(numeral_system, *number) == NULL ) + return false; + ++number; + } + + return true; +} + int main(int argc, char* argv[]) { @@ -199,6 +215,13 @@ main(int argc, char* argv[]) /* Number variables to be converted */ char* number = argv[1]; + /* Check if number belongs to it's numerical system */ + if( !is_valid_number(from, number) ) + { + fprintf(stderr, "error: %s.\n", strerror(EDOM)); + exit(EDOM); + } + /* _first and _last variables */ char* from_first = from; char* from_last = from + (strlen(from) - 1); -- cgit 1.4.1-2-gfad0