| ||||||||||||||||||||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Checking if string is a number or not
I've made a function that determines if character string is a number or not.Using only isdigit().
Here is my sample code Code:
#include <string.h>
#include <stdio.h>
#include <ctype.h>
int isNumber(char str[])
{
int withDecimal =0,isNegative=0 ,i=0;
int len = strlen(str);
for (i=0; i<len; i++)
{
if (!isdigit(str[i])) // if1
{
if (str[i] == '.')
{
if(withDecimal){
return 0;
}
withDecimal =1;
}
else if (str[i] == '-')
{
if(isNegative){
return 0;
}
if(i==0){
isNegative = 1;
}else{
return 0;
}
}else{
return 0;
}
} //end if1
}// end for
return 1;
}
int main(){
//for testing purpose only
char charNum[10][15] = {
"100.23" ,
"-200.456",
"200..",
"..200",
"4.9.9.6",
"--500",
"500--",
"59-52-6",
"50.69-5",
"5000"
};
int cntr = 0;
for(cntr=0; cntr<10; cntr++){
printf("%s\t\t", charNum[cntr]);
if(isNumber(charNum[cntr])){
printf("is a number!\n");
}else{
printf("NOT a NUMBER!\n");
}
}
}
and here is the output Code:
100.23 is a number! -200.456 is a number! 200.. NOT a NUMBER! ..200 NOT a NUMBER! 4.9.9.6 NOT a NUMBER! --500 NOT a NUMBER! 500-- NOT a NUMBER! 59-52-6 NOT a NUMBER! 50.69-5 NOT a NUMBER! 5000 is a number! as you can see. The function works fine.But to be on the safe side.I need to know your reaction on the function, whether it needs to improve on some aspects (memory utilization or something). regards, Jaro |
|
#2
|
|||
|
|||
|
looks good, nice job. it seems like you're looking for nitpicky complaints, so here you are:
1. isNegative and withDecimal would probably be better as bool types, because (a) you test them as such in if statements and (b) they aren't holding numbers, just a yes/no flag, so a bool would be more intuitive/readable 2. technically, you use strlen() as well as isdigit() 3. this is purely your preference, to make the function more useful, you could have it return the number if it is a number--the issue is then how to indicate failure. you could have it return a boolean and take a result parameter as a pointer/reference to an int. nice job though! |
|
#3
|
|||
|
|||
|
Quote:
Actually the code has lots of bugs.And these are as follows. ".-" , "-." , "." and "-" are considered numbers. "+1.234E-05" is considered as not a number. First I tried to solve the ".-" , "-." , "." and "-" issues.And what I've got is a isNumber() that has the greatest number of if else condition that I ever done. And another issue arrsises the "100.3-" is considered to be a number. And I haven't even started yet on the "+1.234E-05" issue. Furtunately a kind person poited me into using the strtod(). After quick reading on how to use/implement strtod(). so here's the code with strtod().Suprisingly the strtod() made things lot easier. Code:
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
char errorNum[20]="";
char tempVal[100]="";
int isNumber(const char str[]){
char *ok;
strtod(str,&ok);
/*
if(!isspace(*str)){ // check for leading whitespace,
if(strlen(ok)==0){
return 1;
}else{
return 0;
}
}else{
return 0;
}
*/
return !isspace(*str) && strlen(ok)==0;
}
int main(){
//for testing purpose only
char charNum[14][15] = {
"00100" ,
"-00200",
"+030",
"-040.30",
"-000.35",
" 50",
"50 ",
"char",
"200.003-",
".-",
"-",
".",
"+1.234E-05",
"-6.789E+10"
};
int cntr = 0;
for(cntr=0; cntr<14; cntr++){
printf("[%s]\t\t", charNum[cntr]);
if(isNumber(charNum[cntr])){
printf("is a number!\n");
}else{
printf("NOT a NUMBER!\n");
}
}
}
OUTPUT Code:
[00100] is a number! [-00200] is a number! [+030] is a number! [-040.30] is a number! [-000.35] is a number! [ 50] NOT a NUMBER! [50 ] NOT a NUMBER! [char] NOT a NUMBER! [200.003-] NOT a NUMBER! [.-] NOT a NUMBER! [-] NOT a NUMBER! [.] NOT a NUMBER! [+1.234E-05] is a number! [-6.789E+10] is a number! I know that the final revision dosen't use isdigit().The reason why isdigit() is use in the first place is that this is the only function that I know (which is portable) that can detect if (in this case) a character is a number or not. And I just found out that strtod() is also portable. also notice in the isNumber().there are some code that are commented out, this is purely for reading purepuses only. So I guess the lesson that I've learn from this program is that I need to know all the standard C function, thier meaning and ussage. and also lots of people have commented on the way that I've written the code, so I guess that would be it. Regards, Jaro Last edited by jaro : May 18th, 2006 at 05:57 AM. Reason: edit some sentences |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > Checking if string is a number or not |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|