gpt4 book ai didi

c - 检查字母字符的函数

转载 作者:太空宇宙 更新时间:2023-11-04 00:12:57 26 4
gpt4 key购买 nike

我创建了一个函数来检查用户输入的真实姓名是否不包括所有其他非字母字符。嗯,在我这边,作为 C 语言的初学者,它工作得很好。无论如何,我有一个小问题,关于字符串名称,如果该字符串中有空格,我会得到错误的名称,但如果只有一个名称 (michi),则一切正常。

        #include <stdio.h>
#include<string.h>

/* Here is the Function which check if the string contains only: */
/* abcdefghijklmnopqrstuvwxyz and ABCDEFGHIJKLMNOPQRSTUVWXYZ */
int checkName(char *s){
int i,length;
length = (strlen(s));

for (i=0;i<length;i++){
if(s[i] == '0' || s[i] <= '9'){
return 1;
}
}
return 0;
}

int main(){
char name[]= "Michi";
int check;

if((check = checkName(name)) == 0){
printf("\n\n\t\t\tYour name is:\t%s\n\n",name);
}else{
printf("\n\n\t\t\tWrong name:\t%s\n\n",name);
}

return 0;
}

我的问题是:1)我是否找到了一种正确的方法来检查字符串是否仅包含非字母字符。2)我如何扩展我的功能以跳过空格

最佳答案

看看 ctype.h 中的 isalpha。如果 char 是字母,这将返回 true,就像您想要的那样。

http://www.cplusplus.com/reference/cctype/isalpha/

顺便说一句,如果您正在检查 ASCII 编码,您的函数将无法处理诸如“(”或“~”之类的字符。

关于c - 检查字母字符的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31010152/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com