C++判断输入是否为字符,数字,空格等

#include<ctype.h>中包含了一系列字符函数

函数名称 返回值【输入是字符char】
isalnum() 如果是字母或数字,返回true
isalpha() 如果是字母,返回true
isdigit() 如果是数字,返回true
islower() 如果是小写字母,返回true
ispunct()

如果是标点符号,返回true

isspace() 如果是空白字符,包括空格、进纸、换行符、回车、制表符等,返回true
isupper() 如果是大写字符,返回true
tolower() 如果是大写字符,返回其小写
toupper() 如果是小写字符,返回其大写
isxdigit() 如果是16进制数,返回true,如0-9、a-f、A-F
iscntrl() 如果是控制字符,返回true
isgraph() 如果是除空格以外的打印字符,返回true
isprint()

如果是打印字符,返回true

这些函数可以用于字符串操作的遍历中。

#include<iostream>
#include<string>
#include<ctype.h>
using namespace std;
int main()
{
	string s = "abcdd";
	
	int length = s.length();
	for (int i = 0;i < length;i++)
	{
		if (islower(s[i]))
			s[i] = toupper(s[i]);
	}
	cout << s << endl;
	system("pause");
	return 0;
}
//输出:ABCDD

python中字符串是不可变类型,不支持item assignment,只能新生成一个string类型

猜你喜欢

转载自blog.csdn.net/qq_40801709/article/details/106873733