How do i find out if argv[1] is all numbers?
Printable View
How do i find out if argv[1] is all numbers?
Loop through it, seeing if the characters are in the range '0' to '9'. You may need to include '-' for the first character.
Could you supply some code?
Ok this is what i came up with but it doesnt seem to work...
Code:int Digit(char * test)
{
for(int i = 0; i > strlen(test); i++)
{
if(isdigit(test[i]) == 0)
{
return 1;
}
}
return 0;
}
Think you lost your [ i ] there.
I think it was meant to be like this:
Code:int Digit(char * test)
{
for(int i = 0; i > strlen(test); i++)
{
if(isdigit(test[ i ]) == 0)
{
return 1;
}
}
return 0;
}
I had the i their, but it made it italics instead.
Try this:
I think you got your comparison mixed up between > and <.Code:bool Digit(char *test) {
for(int j = 0; j < strlen(test); j++) {
if(isdigit(test[j]) == 0) {
return true;
}
}
return false;
}
I thought I'd try out my asm skills on this one, try this
i reckon that's probably the fastest way of doing it.Code:bool StringNumeric(char* MyString)
{
__asm
{
push ebx
push ecx
push ax
mov ax, 0x2dfe ;ah = '-' al = ('.' - 0x30)
mov ebx, MyString
mov ecx, 0 ;ch must be zero for the jcxz instruction to work (ecx is 0 as we must add it to ebx)
sub ah, [ebx] ;subtract the first character from ah (so ah = 0 if the string begins with a '-')
sete cl ;if the 1st character is '-' ecx = 1, else ecx = 0
add ebx, ecx ;adds ecx to ebx
loopstrt:
mov cl, [ebx] ;store the curren character in cl
inc ebx ;move to the next character
jcxz stringfinished ;if we find a null char exit the loop
sub cl, 0x30 ;cl should be between 30 and 39, so subtract 30 and check it's below 9
cmp cl, 9
jb loopstrt
sub al, cl ;compare cl to al (true if cl was '.' originally, set al to 0 if true
jz loopstrt ;(if it's false al could be anything but we return false anyway
stringfinished:
pop ax
cmp cl, 0 ;if cl = 0 return true else return false
sete al
pop cx
pop ebx
}
}