9

Is there an easy way to call a C script to see if the user inputs a letter from the English alphabet? I'm thinking something like this:

if (variable == a - z) {printf("You entered a letter! You must enter a number!");} else (//do something}

I want to check to make sure the user does not enter a letter, but enters a number instead. Wondering if there is an easy way to pull every letter without manually typing in each letter of the alphabet :)

jww
  • 90,984
  • 81
  • 374
  • 818

7 Answers7

15

It's best to test for decimal numeric digits themselves instead of letters. isdigit.

#include <ctype.h>

if(isdigit(variable))
{
  //valid input
}
else
{
  //invalid input
}
Brian R. Bondy
  • 327,498
  • 120
  • 583
  • 623
12
#include <ctype.h>
if (isalpha(variable)) { ... }
Warren Young
  • 38,927
  • 8
  • 82
  • 96
  • 4
    Worth noting that `isdigit()` is more useful here. – Michael Krelin - hacker Sep 25 '09 at 18:42
  • Awesome. What would it be to check for number and alpha? –  Sep 25 '09 at 18:43
  • what are *, /, -, +, % (math operators) defined as? alpha, numbers, or something else? –  Sep 25 '09 at 18:46
  • If I check alpha and num, do I include another library? –  Sep 25 '09 at 18:50
  • 1
    Math operators are considered punctuation (checked with the function ispunct()). All of these functions are including within ctype.h as the example shows. – Andrew Song Sep 25 '09 at 18:55
  • hmm. how would this look in a case statement? –  Sep 25 '09 at 18:56
  • You can't call functions as case-checkers in C. You'd have to use if/else if/else to test multiple things at once and have each handled differently. – Warren Young Sep 25 '09 at 19:03
  • oh ok, perfect. so if i'm using a switch case but want to check to see if they enter letter/number i'll just put the if/else to check at the top, then put the switch/case underneath. it's finally coming together :) –  Sep 25 '09 at 19:08
4

isalpha() will test one character at a time. If the user input a number like 23A4, then you want to test every letter. You can use this:

bool isNumber(char *input) {
    for (i = 0; input[i] != '\0'; i++)
        if (isalpha(input[i]))
            return false;
    return true;
}

// accept and check
scanf("%s", input);  // where input is a pointer to a char with memory allocated
if (isNumber(input)) {
    number = atoi(input);
    // rest of the code
}

I agree that atoi() is not thread safe and a deprecated function. You can write another simple function in place of that.

Ashwin
  • 3,521
  • 2
  • 17
  • 11
2

Aside from the isalpha function, you can do it like this:

char vrbl;

if ((vrbl >= 'a' && vrbl <= 'z') || (vrbl >= 'A' && vrbl <= 'Z')) 
{
    printf("You entered a letter! You must enter a number!");
}
bbg
  • 2,704
  • 3
  • 18
  • 13
  • What do you have against vowels? If you're going to remove the meaning from your variable names, you might as well just go with 'v'. – Ryan Fox Sep 25 '09 at 21:41
1

The strto*() library functions come in handy here:

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define SIZE ...

int main(void)
{
  char buffer[SIZE];
  printf("Gimme an integer value: ");
  fflush(stdout);
  if (fgets(buffer, sizeof buffer, stdin))
  {
    long value;
    char *check;
    /**
     * strtol() scans the string and converts it to the equivalent 
     * integer value.  check will point to the first character
     * in the buffer that isn't part of a valid integer constant;
     * e.g., if you type in "12W", check will point to 'W'.  
     *
     * If check points to something other than whitespace or a 0
     * terminator, then the input string is not a valid integer. 
     */
    value = strtol(buffer, &check, 0);
    if (!isspace(*check) && *check != 0)
    {
      printf("%s is not a valid integer\n", buffer);
    }
  }
  return 0;
}
John Bode
  • 113,266
  • 18
  • 112
  • 190
1

You can also do it with few simple conditions to check whether a character is alphabet or not

if((ch>='a' && ch<='z') || (ch>='A' && ch<='Z'))
{
    printf("Alphabet");
}

Or you can also use ASCII values

if((ch>=97 && ch<=122) || (ch>=65 && ch<=90))
{
    printf("Alphabet");
}
Pankaj Prakash
  • 2,136
  • 24
  • 29
0
int strOnlyNumbers(char *str)
{
 char current_character;
 /* While current_character isn't null */
 while(current_character = *str)
 {
  if(
     (current_character < '0')
    ||
     (current_character > '9')
    )
  {
   return 0;
  }
  else
  {
   ++str;
  }
 }
 return 1;
}