Path: utzoo!utgpu!watmath!clyde!att!osu-cis!tut.cis.ohio-state.edu!cwjcc!hal!nic.MR.NET!xanth!mcnc!rti!sunpix!matthew From: matthew@sunpix.UUCP ( Sun NCAA) Newsgroups: comp.lang.c Subject: #include Keywords: something extra... Message-ID: <354@greens.UUCP> Date: 20 Dec 88 16:17:33 GMT Organization: Sun Microsystems, Research Triangle Park, NC Lines: 60 I'm only a beginner 'C' programmer, but for a recent technical college midterm exam, I had to write a program that would read in strings from the keyboard, detect if they contained valid floating point numbers, count the total number of entries, the total number of valid floating point entries and as output print the count of the total number of entries, the total number of valid entries and the average of the total valid entries and a decending sorted list of valid entries. All this was quite simple, and of course I used gets(); to get the an input string and atof(); to do the string to float conversion. But no where could I find a function to validate the string to see if it did contain a number that atof(); would correctly interpret (0.0 is a valid floating point number and I did to be able to enter it). After studying 'atof();' in K & R, I put this together. It so easy, I wonder why I haven't seen it before. [BTW, isfloat(); can double for isdouble(); as can isint(); for islong();] int isfloat(str) int isspace(char); int isdigit(char); char str[]; { int i; for ( i = 0; isspace(str[i]); i++) ; /* eliminate leading whitespace */ if (str[i] == '+' || str[i] == '-') i++; /* if '+' or '-' skip to next char */ if (str[i] == '.') i++; /* if '.' skip to next char */ if (isdigit(str[i])) return(1); /* if digit, return 'true' */ return(0); /* not a float, return 'false' */ } int isint(str) int isspace(char); int isdigit(char); char str[]; { int i; for ( i = 0; isspace(str[i]); i++) ; /* eliminate leading whitespace */ if (str[i] == '+' || str[i] == '-') i++; /* if '+' or '-' skip to next char */ if (isdigit(str[i])) return(1); /* if digit, return 'true' */ return(0); /* not a int, return 'false' */ } -- Matthew Lee Stier (919) 469-8300| Sun Microsystems --- RTP, NC 27560| "Wisconsin Escapee" uucp: {sun, rti}!sunpix!matthew |