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類型

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章