diff options
author | Daniel Santos <dacs.git@brilhante.top> | 2022-03-11 23:26:07 +0000 |
---|---|---|
committer | Daniel Santos <dacs.git@brilhante.top> | 2022-03-12 12:16:32 +0000 |
commit | 06bb8994d4675495e3fbd660378ec370337c884d (patch) | |
tree | 5a4e1e675fd236dec39da3fac636b8b1cad2bddb | |
parent | ab849603a03eff72f26b7150e2808c5051bf2ea1 (diff) | |
download | numericx-c-06bb8994d4675495e3fbd660378ec370337c884d.tar.gz |
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 <dacs.git@brilhante.top>
-rw-r--r-- | numericx.c | 23 |
1 files changed, 23 insertions, 0 deletions
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); |