检查用户是否在 C 中输入字母或数字

发布于 2024-08-05 15:32:49 字数 257 浏览 7 评论 0原文

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

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

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

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 :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

半步萧音过轻尘 2024-08-12 15:32:49

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

#include <ctype.h>

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

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
}
二货你真萌 2024-08-12 15:32:49
#include <ctype.h>
if (isalpha(variable)) { ... }
#include <ctype.h>
if (isalpha(variable)) { ... }
月竹挽风 2024-08-12 15:32:49

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() 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.

亚希 2024-08-12 15:32:49

除了 isalpha 函数之外,您还可以这样做:

char vrbl;

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

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!");
}
八巷 2024-08-12 15:32:49

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

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;
}
小糖芽 2024-08-12 15:32:49
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;
}
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;
}
你是我的挚爱i 2024-08-12 15:32:49

您还可以通过一些简单的条件来检查是否字符是否为字母

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

或者您也可以使用 ASCII 值

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

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");
}
往事风中埋 2024-08-12 15:32:49

您可以实现以下返回布尔值的函数,它检查输入是否仅由字符而不是数字组成,它还会忽略空格。请注意,它假设输入是由 fgets 而不是 scanf 收集的。仅当您想使用其他输入法时才应更改 while 条件。

bool is_character(char text[])
{
    bool just_letters;
    just_letters = true;
    while((text[i] != '\n') && (just_letters == true))
    {
        if ((text[i] >= 'A' && text[i] <= 'Z') || (text[i] >= 'a' && text[i] <= 'z') || text[i] == 32)
        {
            just_letters = true;
        }
        else
        {
            just_letters = false;
        }
    }
    return just_letters;
}

You can implement the following function that returns a boolean, it checks whether the input is only composed by characters and not numbers, it also ignores spaces. Note that it supposes that the input in collected by fgets and not scanf. You should only change the while condition if you want to use another input method.

bool is_character(char text[])
{
    bool just_letters;
    just_letters = true;
    while((text[i] != '\n') && (just_letters == true))
    {
        if ((text[i] >= 'A' && text[i] <= 'Z') || (text[i] >= 'a' && text[i] <= 'z') || text[i] == 32)
        {
            just_letters = true;
        }
        else
        {
            just_letters = false;
        }
    }
    return just_letters;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文