静态方法输出参数统计大写字母、小写字母、数字、其他字符的个数

来源:互联网 发布:门锁软件v8 编辑:程序博客网 时间:2024/05/27 09:48

问题及代码:

/*      * Copyright (c) 2016, 烟台大学计算机与控制工程学院      * All rights reserved.      * 文件名称:date.cpp                            * 作    者:单昕昕                                  * 完成日期:2016年3月31日      * 版 本 号:v2.0                   * 问题描述:输入一个字符串,写一个静态方法来统计大写字母、小写字母、数字、其他字符的个数。 * 程序输入:一个字符串。  * 程序输出:字符串中大写字母、小写字母、数字、其他字符的个数。 */using System;using System.Text;namespace ConsoleApplication1{    class Program    {        static void Main(string[] args)        {            Console.Write("s=");            string str = Console.ReadLine();//输入字符串            //Console.WriteLine(str.Length);            int a,b,c,d;            CalString.calculate(str, out a, out b, out c, out d);//调用静态方法来计算            Console.WriteLine("大写字母={0},小写字母={1},数字={2},其他字符的个数={3}", a, b, c, d);            Console.ReadKey();        }    }    class CalString    {        public static void calculate(string s, out int n1, out int n2, out int n3, out int n4)        {            n1 = n2 = n3 = n4 = 0;//大写字母、小写字母、数字、其他字符的个数            char[] ss = s.ToCharArray();//将字符串转换成字符数组            foreach (char item in ss)            {                if (item >= 'A' && item <= 'Z') ++n1;//大写字母                else if (item >= 'a' && item <= 'z') ++n2;//小写字母                else if (item >= '0' && item <= '9') ++n3;//数字                else ++n4;//其他字符            }        }    }}


 


运行结果:


好。。简单。。阿。。

就是又套了个类和输出参数的帽子。。其他的和以前写C++的一模一样。。

0 0