تحقق مما إذا كان المستخدم يدخل حرف أو رقم في ج

StackOverflow https://stackoverflow.com/questions/1478932

  •  16-09-2019
  •  | 
  •  

سؤال

هل هناك طريقة سهلة لاستدعاء برنامج نصي C لمعرفة ما إذا كان المستخدم يدخل حرف من الأبجدية الإنجليزية؟ أنا أفكر بشيء مثل هذا:

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

أريد التحقق للتأكد من عدم إدخال المستخدم حرفا، ولكن يدخل رقما بدلا من ذلك. أتساءل عما إذا كانت هناك طريقة سهلة لسحب كل حرف دون كتابة يدويا في كل حرف من الحروف الأبجدية :)

هل كانت مفيدة؟

المحلول

#include <ctype.h>
if (isalpha(variable)) { ... }

نصائح أخرى

من الأفضل اختبار الأرقام الرقمية العشرية أنفسهم بدلا من الحروف. Isdigit..

#include <ctype.h>

if(isdigit(variable))
{
  //valid input
}
else
{
  //invalid input
}

سوف Isalpha () اختبار شخصية واحدة في وقت واحد. إذا قام المستخدم بإدخال رقم مثل 23A4، فأنت تريد اختبار كل حرف. يمكنك استخدام هذا:

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
}

أوافق على أن ATOI () ليس مؤمنا آمنا ووظيفة مهتنة. يمكنك كتابة وظيفة أخرى بسيطة بدلا من ذلك.

بصرف النظر عن وظيفة Isalpha، يمكنك القيام بذلك مثل هذا:

char vrbl;

if ((vrbl >= 'a' && vrbl <= 'z') || (vrbl >= 'A' && vrbl <= 'Z')) 
{
    printf("You entered a letter! You must enter a number!");
}

ال strto*() وظائف المكتبة تأتي في متناول يدي هنا:

#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;
}

يمكنك أيضا القيام بذلك مع عدد قليل من الظروف البسيطة ل تحقق ما إذا كانت شخصية الأبجدية أم لا

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

أو يمكنك أيضا استخدام قيم ASCII

if((ch>=97 && ch<=122) || (ch>=65 && ch<=90))
{
    printf("Alphabet");
}
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;
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top