>>can you show me how I could use the counter for this?
I can do better, I can show you a more thorough way to do it :-)
Code:
#include <stdio.h>
#include <string.h>
typedef enum {FALSE, TRUE} bool_t;
static char *digits = "0123456789";
static char *alphas = "abcdefghijklmnopqrstuvwxyz"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ";
static char *others = "~!@#$%^&*()|\\[]{} ,/<>?:\";'";
static bool_t contains(char *, char *);
static bool_t more_than_one(char *, char *);
int main(void)
{
while (1)
{
char s[20], *p;
bool_t is_int = FALSE, is_float = FALSE;
printf("Enter a number or EOF to quit: ");
fgets(s, sizeof s, stdin);
if ((p = strchr(s, '\n')) != 0)
{
*p = '\0';
}
if (strcmp(s, "EOF") == 0)
{
break;
}
if (contains(s, alphas) || contains(s, others))
/* Check for validity */
{
/* Do nothing, both are already false */
}
else if (contains(s, digits) && contains(s, "."))
/* Check for simple floating-point */
{
if (!more_than_one(s, "."))
{
is_float = TRUE;
}
}
else if (contains(s, digits))
/* Check for int */
{
is_int = TRUE;
}
printf("%s is:\n", s);
printf("\t%san int\n", (is_int) ? "" : "NOT ");
printf("\t%sa float\n", (is_float) ? "" : "NOT ");
}
return 0;
}
bool_t contains(char *src, char *c)
{
if (strpbrk(src, c) != 0)
{
return TRUE;
}
return FALSE;
}
bool_t more_than_one(char *src, char *c)
{
char *found = strpbrk(src, c);
if (found != 0 && strpbrk(found+1, c) != 0)
{
return TRUE;
}
return FALSE;
}