0

I would like to understand how to validate a string input and check whether the entered string is Numeric or not? I belive isdigit() function is the right way to do it but i'm able to try it out with one char but when it comes to a string the function isn't helping me.This is what i have got so far,Could any please guide me to validate a full string like

char *Var1 ="12345" and char *var2 ="abcd"

#include <stdio.h>
#include <ctype.h>

int main()
{
   char *var1 = "hello";
   char *var2 = "12345";

   if( isdigit(var1) )
   {
      printf("var1 = |%s| is a digit\n", var1 );
   }
   else
   {
      printf("var1 = |%s| is not a digit\n", var1 );
   }
   if( isdigit(var2) )
   {
      printf("var2 = |%s| is a digit\n", var2 );
   }
   else
   {
      printf("var2 = |%s| is not a digit\n", var2 );
   }

   return(0);
}

The program seems to be working fine when the variables are declared and initialized as below,

int var1 = 'h'; int var2 = '2';

But i would like to understand how to validate a full string like *var =" 12345";

2
  • Read the documentation of isdigit! It works on single characters only. (And so the next step ought to be logical.)
    – Jongware
    Jan 10, 2015 at 18:12
  • You need to iterate over array, apply isdigit to each character and check return value.
    – tumdum
    Jan 10, 2015 at 18:13

3 Answers 3

1

Try to make a loop on each string and verify each char alone

1

isdigit takes a single char, not a char*. If you want to use isdigit, add a loop to do the checking. Since you are planning to use it in several places, make it into a function, like this:

int all_digits(const char* str) {
    while (*str) {
        if (!isdigit(*str++)) {
            return 0;
        }
    }
    return 1;
}

The loop above will end when null terminator of the string is reached without hitting the return statement in the middle, in other words, when all characters have passed the isdigit test.

Note that passing all_digits does not mean that the string represents a value of any supported numeric type, because the length of the string is not taken into account. Therefore, a very long string of digits would return true for all_digits, but if you try converting it to int or long long you would get an overflow.

1
  • This one seems to be the simplest solution.Thanks guys:) Jan 10, 2015 at 18:44
0

Use this

int isNumber(const char *const text)
{
    char *endptr;
    if (text == NULL)
        return 0;
    strtol(text, &endptr, 10);

    return (*endptr == '\0');
}

then

if (isNumeric(var1) == 0)
    printf("%s is NOT a number\n", var1);
else
    printf("%s is number\n", var1);

the strtol() function will ignore leading whitspace characters.

If a character that cannot be converted is found, the convertion stops, and endptr will point to that character after return, thus checking for *endptr == '\0' will tell you if you are at the end of the string, meaning that all characters where successfuly converted.

If you want to consider leading whitespaces as invalid characters too, then you could just write this instead

int isNumber(const char *text)
{
    char *endptr;
    if (text == NULL)
        return 0;
    while ((*text != '\0') && (isspace(*text) != 0))
        text++;
    if (*text == '\0')
        return 0;
    strtol(text, &endptr, 10);

    return (*endptr == '\0');
}

depending on what you need, but skipping leading whitespace characters is to interpret the numbers as if a human is reading them, since humans "don't see" whitespace characters.

2
  • strtol skips leading whitespace, so the question for the OP is if that is allowed or not.
    – Jongware
    Jan 10, 2015 at 18:19
  • Thanks a bunch for sharing code snippet and for explaining the logic as well.I shall understand the logic first and try it out and let you know the result in a moment :) Jan 10, 2015 at 18:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.