#include <is.h>
Sun NCAA
matthew at sunpix.UUCP
Wed Dec 21 02:17:33 AEST 1988
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 |
More information about the Comp.lang.c
mailing list