I'm a beginning C programmer. I need a function that will take a string and determine if it's a number. The data is coming from files created by different people and the number formats WILL vary. So the function needs to decide if it was Supposed to be a number and if so, I will call the right conversion function (atoi/strtoi, atol/strtol etc etc.) So it needs to consider ints and floats, some with dollar signs, some with commas, some not. If a human would recognize it as a number the function should too.
The function works exactly as I want it to, and I haven't been able to break it no matter what I throw at it, but I'm sure there are better ways to do the job, and I'm equally sure there are problems with the way I'm doing it that I just am too inexperienced to know about yet. So Please, take a look and let me know of any potential pitfalls I may encounter with this function, feel free to suggest alternative ways if you're inclined.
Here's what I have come up with. As I've said it works, the example takes one or more command line args to simplify testing and there is no error checking yet, I will add that to the working program, but for function testing in a controlled setting I'm looking purely for functionality and problems that I might not know about with the way I'm handling the function.
Since the $ is significant to the shell it has to be enclosed in quotes but that won't be a problem when implemented because the data won't be coming from the command line.
Anything I should be aware of, or should consider with this code? More efficient mechanisms for accomplishing this would be appreciated. If there is a library function for this it has escaped my detection but would love to pointed in the right direction.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int check_is_numeric(const char *arr)
{
//define valid characters for any numeric value
char numdig[] = {'0', '1', '2', '3', '4', '5',
'6', '7', '8', '9', ',', '.', '$'};
int j=0;
int notnum = 0;
int hasdec = 0;
while (arr[j] != '\0') {
for(int b = 0; b < 13;b++){
//use hasdec to indicate a decimal in the string
//but don't allow more than one.
if( arr[j] == numdig[b] && hasdec < 2) {
if (b == 11) {++hasdec;}
notnum =0;
break;
} else {
notnum =1;
}
}
if(notnum){break;}
++j;
}
if(!notnum) {
if(!hasdec){return 1;}
else {return 2;}
} else {
return 0;
}
}
int main(int argc, char *argv[])
{
char *argvarray[argc];
for(int j=0; j< argc; j++){
argvarray[j] = malloc(strlen(argv[j] + 1));
strcpy(argvarray[j], argv[j]);
}
int i = 0;
for(int j=1; j< argc; j++){
//pass each arg to the function
//function should accept anything a human would recognize as a number
//and return 0 for non number, 1 for int and 2 for float
i = check_is_numeric(argvarray[j]);
switch(i) {
case 0:
printf("%s is not a number.\n", argvarray[j]);
break;
case 1:
printf("%s is an integer.\n", argvarray[j]);
break;
case 2:
printf("%s is a floating point number.\n", argvarray[j]);
break;
}
}
return 0;
}
e|E
which can be followed by a+|-
or leading 'b|B for binary or leading0x|0X
for hexidecimal (the leading0x
number can also contain:a|A
b|B
c|C
d|D
e|E
andf|F
for digits and what about a leading+|-
? And there is a trailingH|h
for hexidecimal numbers. – user3629249 21 hours ago