0

Need to write a program to take input from a user in the form of an int and validate that what they entered is not a char from a-z.

Is there another way to do this other than:

if((num != 'a') && (num != 'b') && (num != 'c') && (num != 'd') etc.....) printf("You entered %d", num);

4
  • You should be care of the negative number which has - as a character ahead. Commented Mar 17, 2014 at 3:04
  • stackoverflow.com/questions/4072190/… Commented Mar 17, 2014 at 3:05
  • Your question is ambiguous. It appears that you might be able to use: int num; if (scanf("%d", &num) == 1 && !islower(num)) printf("You entered %d\n", num); (noting the newline in the output). If the input is inherently non-numeric, the scanf() will fail; if the input is numeric but in the range of lower-case letters (97..122 in codesets derived from ISO 8859), then it will not be accepted. If you're using int num = getchar(); to read a single character, the answer is somewhat different. Commented Mar 17, 2014 at 3:12
  • 1
    Accept the answer and set thy free. Commented Mar 17, 2014 at 3:13

4 Answers 4

4

Yes, use the isdigit() function provided in the ctype.h header file

Sign up to request clarification or add additional context in comments.

1 Comment

Dang, you bet me by 30secs
1

Rather than trying to work out that it's not a character, work out that it is a digit and discard the rest using the isdigit function from ctype like below.

#include <ctype.h>

...

if (isdigit(num)) {
  printf("You entered %d\n", num);
}

But this only works on single characters which is quite useless when you read in strings. So instead you could instead use the function sscanf. Like this.

int num;
if (sscanf(numstr, "%d", &num)){
  printf("You entered %d\n", num);
} else {
  printf("Invalid input '%s'\n", numstr);
}

Another option is to use the atoi function. But as I recall it doesn't handle errors and I find quite inferior to sscanf.

Comments

1

You can use the isalpha() function in the "ctype.h" header. It returns true if it is a letter. So you could do something like:

if ( !isalpha() )
{
    // Do whatever
}

Here is a link to the documentation for more information.

2 Comments

Assuming he'd be fine with anything except numbers, which I kinda doubt, since he mentions int. isdigit() would be a better solution.
@Saraph I originally was going to go with using that, but I re-read the question and must have interpreted it wrong. You are correct.
-1
#include <stdio.h>
int main()
{
    char c;
    printf("Enter a character: ");
    scanf("%c",&c);
    if( (c>='a'&& c<='z') || (c>='A' && c<='Z'))
       printf("%c is an alphabet.",c);
    else
       printf("%c is not an alphabet.",c);
    return 0;
}

5 Comments

The variable must be declared as an integer, not a char.
This answer wasn't very different from the solution OP originally proposed.
Theoretically, your alphabetic check could capture non-alphabetic characters. If you were on an IBM mainframe that used EBCDIC, then there are non-alpha characters between the codes for 'a' and 'z' — and the difference 'z' - 'a' is not 25 in EBCDIC. Use the isalpha() macro from <ctype.h>.
@Saraph: this solution is a lot more succinct than 26 equality comparisons.
@JonathanLeffler Okay, I'm assuming OP would eventually find out about <, > operators.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.