您的位置:首页 > 其它

习题 8.8 输入一行文字,找出其中大写字母、小写字母、空格、数字以及其他字符各有多少。

2018-02-23 19:59 906 查看

C程序设计(第四版) 谭浩强 习题8.8 个人设计

习题 8.8 输入一行文字,找出其中大写字母、小写字母、空格、数字以及其他字符各有多少。

代码块:

#include <stdio.h>
void function(char *s, int (*p)(char *st));    //定义功能函数
int big(char *st);                             //定义大写字母函数
int small(char *st);                           //定义小写字母函数
int space(char *st);                           //定义空格函数
int number(char *st);                          //定义数字函数
int other(char *st);                           //定义其他字符函数
int main()
{
char str[20], *p=str;
printf("Please enter string: ");           //输入字符串
gets(p);
function(p, big);                          //调用功能函数
function(p, small);
function(p, space);
function(p, number);
function(p, other);
return 0;
}
//功能函数
void function(char *s, int (*p)(char *st))
{
int out;
out=(*p)(s);
printf("%d\n", out);
}
//大写字母函数
int big(char *st)
{
for (int i=0; *st; *st>='A'&&*st<='Z' ? i++, st++ : st++);
printf("Big=");
return i;
}
//小写字母函数
int small(char *st)
{
for (int i=0; *st; *st>='a'&&*st<='z' ? i++, st++ : st++);
printf("Small=");
return i;
}
//空格函数
int space(char *st)
{
for (int i=0; *st; (*st==' ')||(*st=='\t')||(*st=='\n') ? i++, st++ : st++);
printf("Space=");
return i;
}
//数字函数
int number(char *st)
{
for (int i=0; *st; (*st>='0')&&(*st<='9') ? i++, st++ : st++);
printf("Number=");
return i;
}
//其他字符函数
int other(char *st)
{
for (int i=0; *st; (*st<'A'||(*st>'Z'&&*st<'a')||*st>'z')&&(*st<'0'||*st>'9')&&(*st!=' '&&*st!='\t'&&*st!='\n') ? i++, st++ : st++);
printf("Other=");
return i;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐