在ascII表中键盘可输入字符是连续的,不包括空格是从33到126,所以可以定义一个128的数组,做个简单hash,输入的字符作hash key。实现如下:
#include
int main()
{
char a[1024] = {0}; /*接收输入的字符,由于gets不检查长度,所以弄大点*/
int hash_tbl[128] = {0};/*hash 表*/
int i;
printf("Please input string:\n");
gets(a);/*从标准输入读字符*/
for(i=0; a[i]!='\0'; i++) {
hash_tbl[(int)a[i]]++;/*计算输入字符的个数*/
}
/*打印非零字符的个数*/
for(i=33; i<126; i++) {
if (hash_tbl[i]>0) {
printf("%c num is: %d\n", i, hash_tbl[i]);
}
}
return 0;
}
C语言字符串的学习,输入指定字符串,并且计算字符串的位数
#include
#include
int letter,digit,space,others; //全局变量
main()
{
int count(char str[]);
char text[80];
printf("\ninput string:\n");
gets(text);
printf("string:\n");
puts(text);
letter=0;
digit=0;
space=0;
others=0;
count(text);
printf("letter:%d, digit:%d, space:%d, others:%d\n",letter,
digit,space,others);
}
count(char str[])
{
int i;
for(i=0;str[i]!='\0';i++)
if( (str[i]>='a'&&str[i]<='z') || (str[i]>='A'&&str[i]<='Z') )
letter++;
else if( str[i]>='0'&&str[i]<='9')
digit++;
else if(str[i]==' ')
space++;
else others++;
}
#define N 100
#include
#include
main()
{
char a[N];
int j;
gets(a);
j=strlen(a);
printf("%d",j);
}
C语言教程上面都有着道例题,我拿手机上网不好编程,自己找本看吧