zoukankan      html  css  js  c++  java
  • 【编程小题目6】字符数统计

    题目:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。 

    #include <iostream>
    #include <string>
    using namespace std;

    int main()
    {
      const int size = 100;
      char ch[size];
      int CharNum = 0, DigNum = 0, NullNum = 0, OtherNum = 0; 
      for(int i = 0; i < size; i++)
      {
        ch[i] = getchar();
        if(ch[i] == ' ')   //终止输入
        {
          break;
        }
      }

      for(int i = 0; i < size && ch[i] != ' '; i++)
      {
        if( ch[i] >= 'a' && ch[i] <= 'z' || ch[i] >= 'A' && ch[i] <= 'Z')
        {
          CharNum++;
        }
        else if(ch[i] >= '0' && ch[i] <= '9')
        {
          DigNum++;
        }
        else if(ch[i] == ' ')
        {
          NullNum++;
        }
        else
        {
          OtherNum++;
        }
      }
      cout << "字母个数: " << CharNum << endl << "数字个数:" << DigNum << endl 
          << "空格个数: " << NullNum << endl << "其他字符: " << OtherNum << endl;

      return 0;
    }

  • 相关阅读:
    1.8 Hello World添加menu
    1.7 HelloWorld 添加视图
    1.6 Hello World
    1.5 组件开发基础
    awk
    sed
    grep / egrep
    Shell基础知识
    和管道符有关的命令
    Shell变量
  • 原文地址:https://www.cnblogs.com/FoxShark/p/4440917.html
Copyright © 2011-2022 走看看