• 武汉科技大学ACM :1003: 零起点学算法67——统计字母数字等个数


    Problem Description

    输入一串字符,统计这串字符里的字母个数,数字个数,空格字数以及其他字符(最多不超过100个字符)

    Input

    多组测试数据,每行一组

    Output

    每组输出一行,分别是字母个数,数字个数,空格字数以及其他字符个数

    Sample Input

    I am a student in class 1.
    I think I can!

    Sample Output

    18 1 6 1
    10 0 3 1
    

    HINT

    char str[100];//定义字符型数组



    while(gets(str)!=NULL)//多组数据

    {



    //输入代码



    for(i=0;str[i]!='';i++)//gets函数自动在str后面添加''作为结束标志

    {

    //输入代码

    }



    //字符常量的表示,

    'a'表示字符a;

    '0'表示字符0;



    //字符的赋值

    str[i]='a';//表示将字符a赋值给str[i]

    str[i]='0';//表示将字符0赋值给str[i]

    }

    我的代码:

     1 #include<stdio.h>
     2 int main()
     3 {    
     4     char ch[100];
     5     while(gets(ch)!=NULL)
     6 
     7     {
     8         int char_num=0,kongge_num=0,int_num=0,other_num=0,i;
     9         for(i=0;ch[i]!='';i++)
    10 
    11         {
    12             if(ch[i]>='A' && ch[i]<='Z' || ch[i]<='z' && ch[i]>='a')
    13             {
    14                 char_num++;
    15             }
    16             else if(ch[i]==' ')
    17             {
    18                 kongge_num++;
    19             }
    20             else if(ch[i]>='0'&&ch[i]<='9')
    21             {
    22                 int_num++;
    23             }
    24             else
    25             {
    26                 other_num++;
    27             }
    28         }
    29     printf("%d %d %d %d
    ",char_num,int_num,kongge_num,other_num);
    30     }
    31     return 0;
    32 } 

    其他代码:

     1 #include<stdio.h>
     2 #include<string.h>
     3 int main()
     4 {
     5     char str[100];//定义字符型数组
     6     int a=0, b=0, c=0, d=0;//字母,数字,空格,和其它
     7     while (gets(str) != NULL){
     8         int length = strlen(str);
     9         for (int i = 0; i < length; i++){
    10             if (str[i] >= 'A'&&str[i] <= 'Z'||str[i] >= 'a'&&str[i] <= 'z'){
    11                 a++;
    12             }
    13             else if (str[i] == ' '){
    14                 c++;
    15             }
    16             else if (str[i] >= '0'&&str[i] <= '9'){
    17                 b++;
    18             }
    19             else{
    20                 d++;
    21             }
    22         }
    23         printf("%d %d %d %d
    ", a, b, c, d);
    24         a=0; b=0; c=0; d=0;
    25     }
    26     return 0;
    27 }
     1 #include <iostream>
     2 #include<cctype>
     3 using namespace std;
     4 
     5  char s[100];
     6 int main()
     7 {
     8     int alpha,num,space,other;
     9    while(gets(s))
    10    {
    11        alpha=0,num=0,space=0,other=0;
    12        for(int i=0;s[i]!='';++i)
    13        {
    14            if(isalpha(s[i]))      ++alpha;
    15            else if(isdigit(s[i])) ++num;
    16            else if(s[i]==32)      ++space;
    17            else                   ++other;
    18        }
    19        cout<<alpha<<" "<<num<<" "<<space<<" "<<other<<endl;
    20    }
    21     return 0;
    22 }
  • 相关阅读:
    ThinkPHP如果表名有下划线需要用Model应该怎么做?
    JS三级联动实例
    Sublime的使用!emmet常用快捷键梳理
    MUI极简的JS函数
    Atitit 自然语言处理原理与实现 attilax总结
    Atitit 自然语言处理原理与实现 attilax总结
    Atitit  自动化gui 与 发帖机 技术
    Atitit  自动化gui 与 发帖机 技术
    Atitit 衡量项目的规模
    Atitit 衡量项目的规模
  • 原文地址:https://www.cnblogs.com/liuwt365/p/4147404.html
Copyright © 2020-2023  润新知