cctype (ctype.h)常用方法记录

在C++中,有一个处理单个字符的包(从C中来的)。
#include <cctype> (ctype.h)
有一些常见功能,例如判断是否大写字母,是否小写字母,是否数字登等,还有变换,将大写字母变成小写字母,小写字母变成大写字母。
记录一些常见的函数使用,首先我们要知道,这些函数的返回都是 int类型,因此在使用时候需要记得。

#include<cctype>
#include<string>
using namespace std;
int main()
{
    
    
	string s = "Hello";
	for (auto& ch : s) {
    
    
		if (isupper(ch)) {
    
    
			ch = tolower(ch);
		}
	}
	cout << s << endl;
	//输出 hello
	return 0;
}
判断是否是大写字母 int isupper(int c);
判断是否是小写字母 int islower(int c);
判断是否是字母     int isalpha(int c);
判断是否是十进制数字 int isdigit(int c);
判断是否是数字字母 int isalnum(int c);


转变函数:
小写字母转变为大写字母 int toupper(int c);
大写字母转变魏小写字母 int tolower(int c);
实现原理:
  小写转大写   ch - 'a' + 'A';
  大写转小写   ch - 'A' + 'a';

cctype存在的判别字符函数
cctype存在的判别字符函数
在此 也记录一下字母数字常用的ASCII码

  • 字符  十进制 十六进制
    
  • 0    48    0x30
    
  • A    65    0x41
    
  • Z    90    0x5A
    
  • a    97    0x61
    
  • z    122   0x7A
    

一共有26个小写字母

猜你喜欢

转载自blog.csdn.net/JACKSONMHLK/article/details/114002502