我有一个字符串结构(名字姓氏地址等)。
我需要确保第一个字符串(名称)中没有数字。我一直在尝试不同的方法,但都是徒劳的。有什么帮助吗?:/
顺便说一句,我是新来的。非常感谢帮助。
您可以使用 中的isdigit
功能<ctype.h>
。
#include <ctype.h>
/* Return 1 if the name is valid, 0 otherwise. */
int check_surname(const char *name)
{
for (int i = 0; name[i] != '\0'; 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 字符集
十进制数字:
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;
}