【问题标题】:Check if User Inputs a Letter or Number in C检查用户是否在 C 中输入了字母或数字
【发布时间】:2009-09-25 18:38:27
【问题描述】:

有没有一种简单的方法可以调用 C 脚本来查看用户是否输入了英文字母表中的字母?我在想这样的事情:

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

我想检查以确保用户没有输入字母,而是输入了数字。想知道是否有一种简单的方法可以在不手动输入字母表的每个字母的情况下提取每个字母:)

【问题讨论】:

    标签: c validation alphanumeric


    【解决方案1】:

    最好测试十进制数字本身而不是字母。 isdigit.

    #include <ctype.h>
    
    if(isdigit(variable))
    {
      //valid input
    }
    else
    {
      //invalid input
    }
    

    【讨论】:

      【解决方案2】:
      #include <ctype.h>
      if (isalpha(variable)) { ... }
      

      【讨论】:

      • 值得注意的是isdigit()在这里更有用。
      • 太棒了。检查数字和字母是什么意思?
      • *、/、-、+、%(数学运算符)定义为什么?字母、数字还是其他?
      • 如果我检查 alpha 和 num,是否包含另一个库?
      • 数学运算符被视为标点符号(使用函数 ispunct() 检查)。如示例所示,所有这些函数都包含在 ctype.h 中。
      【解决方案3】:

      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() 不是线程安全的并且是不推荐使用的函数。您可以编写另一个简单的函数来代替它。

      【讨论】:

        【解决方案4】:

        除了 isalpha 函数,你还可以这样做:

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

        【讨论】:

        • 你有什么反对元音的?如果您要从变量名中删除含义,则最好使用“v”。
        【解决方案5】:

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

        【讨论】:

          【解决方案6】:

          你也可以用几个简单的条件来check whether a character is alphabet or not

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

          或者你也可以使用 ASCII 值

          if((ch>=97 && ch<=122) || (ch>=65 && ch<=90))
          {
              printf("Alphabet");
          }
          

          【讨论】:

            【解决方案7】:
            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;
            }
            

            【讨论】:

              猜你喜欢
              • 2016-02-10
              • 1970-01-01
              • 2015-12-17
              • 1970-01-01
              • 2020-04-30
              • 2014-03-15
              • 2012-08-17
              • 2016-03-22
              • 2023-03-27
              相关资源
              最近更新 更多