华为机考(牛客网)字符统计
#include <stdio.h>
#include <string.h>
#define MAXLEN 500
int acsciinum(const char* str)
{
int len = strlen(str);
if(len > MAXLEN)
return -1;
int ret[128] = {0};
do
{
len--;
int i = 0;
if(*str >= 0 && *str <= 127 )
{
i = *str;
ret[i] = 1;
}
else
continue;
str++;
}while(len > 0);
int j = 0;
int n = 0;
for(j = 0; j<=127;j++ )
{
if(ret[j] == 1)
n++;
}
return n;
}
int main()
{
char cinstr[500];
while(scanf("%s",cinstr)!=EOF)
{
printf("%d",acsciinum(cinstr));
}
}
描述
编写一个函数,计算字符串中含有的不同字符的个数。字符在 ASCII 码范围内( 0~127 ,包括 0 和 127 ),换行表示结束符,不算在字符里。不在范围内的不作统计。多个相同的字符只计算一次
例如,对于字符串 abaca 而言,有 a、b、c 三种不同的字符,因此输出 3 。
数据范围: ![]()
输入描述:
输入一行没有空格的字符串。
输出描述:
输出 输入字符串 中范围在(0~127,包括0和127)字符的种数。
示例1
输入:
abc
输出:
3
示例2
输入:
aaa
输出:
1
浙公网安备 33010602011771号