对字符的理解差不多都在这里了:
#include<stdio.h>
#include<ctype.h> //刚才测试的没调这个库也可以实现
void main(){
printf("单引号:\' 双引号:\" 反斜杠\\ \n");
printf("字符也是整数:A=%d,a=%d\n",'A','a');
printf("a-A=%d\n",'a'-'A');
/*常用函数*/
/*
int isalpha(int ch); // 若ch是字母('A'-'Z','a'-'z')返回非0值,否则返回0。
int isalnum(int ch); // 若ch是字母('A'-'Z','a'-'z')或数字('0'-'9'),返回非0值,否则返回0。
int isdigit(int ch); // 若ch是数字('0'-'9')返回非0值,否则返回0。
int islower(int ch); // 若ch是小写字母('a'-'z')返回非0值,否则返回0。
int isupper(int ch); // 若ch是大写字母('A'-'Z')返回非0值,否则返回0。
int tolower(int ch); // 若ch是大写字母('A'-'Z')返回相应的小写字母('a'-'z')。
int toupper(int ch); // 若ch是小写字母('a'-'z')返回相应的大写字母('A'-'Z')
*/
char Up='F';
char Low='g';
printf("Up=%c Low=%c\n\n",Up,Low);
printf("isalpha:Up %d Low %d\n",isalpha(Up),isalpha(Low));
printf("isalnum:Up %d Low %d\n",isalnum(Up),isalnum(Low));
printf("isdigit:Up %d Low %d\n",isdigit(Up),isdigit(Low));
printf("islower:Up %d Low %d\n",islower(Up),islower(Low));
printf("isupper:Up %d Low %d\n",isupper(Up),isupper(Low));
printf("tolower:Up %c\n",tolower(Up));
printf("toupper:Low %c\n",toupper(Low));
}
对于调用的函数,是几个常用的
对于字符操作基本的函数,man 手册的解释:
SYNOPSIS
#include <ctype.h>int isalnum(int c); int isalpha(int c); int isascii(int c); int isblank(int c); int iscntrl(int c); int isdigit(int c); int isgraph(int c); int islower(int c); int isprint(int c); int ispunct(int c); int isspace(int c); int isupper(int c); int isxdigit(int c);
Feature Test Macro Requirements for glibc (see
feature_test_macros(7)):isascii(): _BSD_SOURCE || _SVID_SOURCE || _XOPEN_SOURCE isblank(): _XOPEN_SOURCE >= 600 || _ISOC99_SOURCE; or cc -std=c99
DESCRIPTION
These functions check whether c, which must have the value of an unsigned char or EOF, falls into a certain character
class according to the current locale.isalnum() checks for an alphanumeric character; it is equivalent to (isalpha(c) || isdigit(c)). isalpha() checks for an alphabetic character; in the standard "C" locale, it is equivalent to (isupper(c) || islower(c)). In some locales, there may be additional characters for which isalpha() is true—letters which are neither upper case nor lower case. isascii() checks whether c is a 7-bit unsigned char value that fits into the ASCII character set. isblank() checks for a blank character; that is, a space or a tab. iscntrl() checks for a control character. isdigit() checks for a digit (0 through 9). isgraph() checks for any printable character except space. islower() checks for a lower-case character. isprint() In some locales, there may be additional characters for which isalpha() is true—letters which are neither upper case nor lower case. isascii() checks whether c is a 7-bit unsigned char value that fits into the ASCII character set. isblank() checks for a blank character; that is, a space or a tab. iscntrl() checks for a control character. isdigit() checks for a digit (0 through 9). isgraph() checks for any printable character except space. islower() checks for a lower-case character. isprint() checks for any printable character including space. ispunct() checks for any printable character which is not a space or an alphanumeric character. isspace() checks for white-space characters. In the "C" and "POSIX" locales, these are: space, form-feed ('\f'), newline ('\n'), carriage return ('\r'), horizontal tab ('\t'), and vertical tab ('\v'). isupper() checks for an uppercase letter. isxdigit() checks for a hexadecimal digits, that is, one of 0 1 2 3 4 5 6 7 8 9 a b c d e f A B C D E F.
这里
int isalpha(int ch);
int isalnum(int ch);
int isdigit(int ch);
返回值不太满意,如果判断为是会返回随机一个数(强迫症无法接受);
所以这里将其改写为bool型加入自建库
/*字符函数*/
/*isalpha判断是否为大小写字母'a'-'z'或'A'-'Z';改为bool型*/
bool Isalpha(int ch){
if(ch>'A'&&ch<'z') return TRUE; //A=65,z=122
else return FALSE;
}
/*isdigit判断是否为数字'0'-'9';改为bool型*/
bool Isdigit(int ch){
if(ch>'0'&&ch<'9') return TRUE;
else return FALSE;
}
/*islower判断是否为小写字母'a'-'z';改为bool型*/
bool Islower(int ch){
if(ch>'a'&&ch<'z') return TRUE;
else return FALSE;
}
/*isupper判断是否为大写字母'A'-'Z';改为bool型*/
bool Isupper(int ch){
if(ch>'A'&&ch<'Z') return TRUE;
else return FALSE;
}
注意这里bool型,C语言对bool是没有返回值的(其实是int型)
所以这里要对bool进行宏定义,不然会报错
#define bool int
#define true 1
#define false 0
int islower(int ch);
int isupper(int ch);
返回值为int型但由于字符=整数,所以没必要修改
可以理解其工作原理是对ch进行了加减
测试函数:
#include<stdio.h>
#include"_public.h"
void main(){
/*测试自建的字符函数*/
printf("输入字符/数字:");
char temp;
scanf("%c",&temp);
printf("temp int %d;char %c\n",temp,temp);
if(Isdigit(temp)) printf("是数字\n");
if(Isalpha(temp)){
printf("是字符\n");
if(Islower(temp)){
printf("是小写字符\n");
printf("转大写:%c\n",toupper(temp));
}
else if(Isupper(temp)){
printf("是大写字符\n");
printf("转小写:%c\n",tolower(temp)); //这里判断没有必要
}
}
}