问题及代码:
输入一个由若干字符组成的字符串,写一个静态方法,方法中使用输出参数输出其中的大写字母,小写字母,数字和其他字符的个数
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string s = Console.ReadLine();
Display(s);
Console.ReadKey();
}
static void Display(string s)
{
char [] c=s.ToCharArray();
int m=0, n=0, x=0, y=0;
foreach (char item in c)
{
if (item >= 48 && item <= 57)
{
x++;
}
else if (item >= 65 && item <= 90)
{
m++;
}
else if (item >= 97 && item <= 122)
{
n++;
}
else y++;
}
Console.WriteLine("大写字母个数为{0},小写字母个数为{1},数字个数为{2},其他字符个数为{3}", m, n, x, y);
}
}
}
运行结果:
知识点总结:
掌握将字符串转换为字符数组的函数,熟练运用asc码