Thread: Checking if the input is a character ?

  1. #16
    Registered User Sir Galahad's Avatar
    Join Date
    Nov 2016
    Location
    The Round Table
    Posts
    277
    Quote Originally Posted by Structure View Post
    This one includes integer fractions ...

    Code:
    int isNumber( char *string ) {
        int oneDot = 0, stringLength = strlen(string), ii;
        for (int i=0; i<stringLength; i++) {
            ii = (int)string[i];
            if ( !(ii >= 48 && ii <= 57) ) {
              if ( ii != '.' && ii != '/') {
                  return 0;
              } else {              
                  if (oneDot++ > 0 || (i < 1 && stringLength == 1)) {
                      return 0;
                  }
              }
            }
        }
        return 1;
    }
    Or maybe just stick to strtol, strtoul, and strtod. They'll do a much better job at detecting valid numeric sequences, not to mention taking care of copying the value into an actual variable.

  2. #17
    Registered User
    Join Date
    Jul 2020
    Posts
    4
    You know , yes you are right , putting all the format specifiers in the first argument of printf() is sucks , its faster to do as you showed.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help checking a character in a structure
    By WranglerYJ in forum C Programming
    Replies: 12
    Last Post: 05-12-2012, 09:05 AM
  2. checking the input is character or not
    By sal in forum C++ Programming
    Replies: 2
    Last Post: 03-11-2012, 06:13 AM
  3. Checking if a character is a '\'
    By serg_yegi in forum C Programming
    Replies: 18
    Last Post: 03-04-2010, 05:42 AM
  4. Checking value of one character in array
    By System_159 in forum C Programming
    Replies: 7
    Last Post: 01-17-2008, 05:05 PM
  5. Checking validity of input character
    By yank in forum C Programming
    Replies: 8
    Last Post: 04-16-2003, 02:42 AM

Tags for this Thread