依据ASCII码统计字符串中不同字符个数

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Lichengguang_std/article/details/81560605

关键函数:

1、ASCII码值:0~177;

2、输出当前字符的ASCII码:(int)s;强制类型转换;

3、输出当前ASCII码对应的字符:(char)65

#include<iostream>
#include<string>
using namespace std;
int main(){
	string s;
	int cont = 0;
	cin >> s;
	int ch[127] = { 0 };
	for (int i = 0; i < s.size(); i++){
		if ((int)s[i] >= 0 && (int)s[i] <= 127){
			if (ch[(int)s[i]] == 0){
				ch[(int)s[i]] = 1;
				cont++;
			}
		}
	}
	cout << cont;
	return 0;
}

猜你喜欢

转载自blog.csdn.net/Lichengguang_std/article/details/81560605
今日推荐