数组一致性-基本的C编程



我有一个字符串结构(姓名地址等)。

我需要确保第一个字符串(名称)中没有数字。我一直在尝试不同的方法,但都无效。任何帮助吗?:/

顺便说一下,我是新来的。非常感谢。

可以使用<ctype.h>中的isdigit函数

#include <ctype.h>
/* Return 1 if the name is valid, 0 otherwise. */
int check_surname(const char *name)
{
  for (int i = 0; name[i] != ''; i++)
  {
    if (isdigit((unsigned char)name[i]))
    {
      return 0;
    } 
  }
  return 1;
}

C11 (n1570),§7.4.1.5 isdigit函数
isdigit函数测试任何十进制数字字符(如5.2.1中定义的)。

C11 (n1570),§5.2.1字符集
10位十进制数字:
0 1 2 3 4 5 6 7 8 9

要检查字符串是否不包含任何数字(十进制)字符,您可以编写如下函数:

#include <ctype.h>
int has_numbers(const char *p)
{
    while (*p) {
        if (isdigit((unsigned char)*p)) {
            return 1;
        }
        p++;
    }
    return 0;
}

最新更新