题目描述
编写一个函数,计算字符串中含有的不同字符的个数。字符在ACSII码范围内(0~127)。不在范围内的不作统计。
输入描述:
输入N个字符,字符在ACSII码范围内。
输出描述:
输出范围在(0~127)字符的个数。
输入例子:
abc
输出例子:
3
【代码一】
/*C++
输入字符,ascii值在[0,127]时插入集合set中,输出set中的元素个数。*/
#include<iostream>
#include<set>
using
namespace
std;
int
main()
{
char
c;
set<
char
> s;
while
(cin>>c)
{
if
(c>=0 && c<=127)
s.insert(c);
}
cout << s.size() <<endl;
}
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str;
getline(cin,str);
int count=0;
for ( int i=0;i<=127;i++)
if (str.find(i)!=string::npos) //值得借鉴
count++;
cout<<count;
}
|
/*编写一个函数,计算字符串中含有的不同字符的个数。字符在ACSII码范围内(0~127)。不在范围内的不作统计*/
//输入描述:输入N个字符,字符在ACSII码范围内(0~127)
//输出描述:输出字符的个数
#include<iostream>
using
namespace
std;
int
main()
{
char
ch;
int
arr[128]={0};
int
count=0;
while
(cin>>ch)
{
if
(ch>=0 && ch<=127)
arr[ch]++;
}
for
(
int
i=0;i<128;i++)
{
if
(arr[i]>0)
count++;
}
cout<<count<<endl;
return
0;
}