第十五周OJ(8)统计字符串种类

来源:互联网 发布:创业公司 程序员 编辑:程序博客网 时间:2024/05/17 03:40


/*

烟台大学计算机与控制工程学院

All  rights  reserved.

作者:汪莹莉

完成时间:2016年12月13日


题目描述

用指针编写一个程序,输入字符串后,统计其中各种字符的个数,输出其中大小写字母,数字,以及其他字符的个数。

主函数已经给出,请编写统计字符种类函数。

输入

一串字符串

输出

该字符串中大小写字母,数字,以及其他字符的个数,最后输出总字符串长度。

样例输入

I play LOL for 3 years.

样例输出

4

12

1

6

23

所给代码:

//C代码
#include <stdio.h>

int main()

{

   char str[100];

   gets(str);

   char *ptr=str;

   void fuction(char *);

   fuction(ptr);

  return 0;

}

编译的程序:

#include <stdio.h>int main(){    char str[100];    gets(str);    char *ptr=str;    void fuction(char *);    fuction(ptr);    return 0;}void fuction(char s[100]){    int a=0,b=0,c=0,d=0,i=0;    while (s[i]!='\0')    {        if(s[i]>='A'&&s[i]<='Z')            a++;        else if(s[i]>='a'&&s[i]<='z')            b++;        else if(s[i]>='0'&&s[i]<='9')            c++;        else            d++;        i++;    }    printf("%d\n%d\n%d\n%d\n%d",a,b,c,d,i);}
运行结果:

知识点总结:解释时将指针换为指向的变量

学习心得;字符串总的长度不用再次引入变量,长度即为i的值


0 0