您的位置:首页 > 编程语言 > C#

C# 上机题 写一个静态方法,输出字符串中大小写字母、数字和其他字符个数

2016-04-01 16:46 656 查看
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{//2.输入一个由若干字符组成的字符串,输出其中的大写字母、小写字母、数字和其他字符的个数。

class Program
{
static void Main(string[] args)
{
Console.Write("请输入一串数字: ");

string s = Console.ReadLine();

char[] chnum = s.ToCharArray();

int count = 0, Count = 0, other = 0 ,figure=0;

for ( int i = 0; i < s.Length; i ++)
{
if ( (char)s[i]>'a'&&(char)s[i]<'z')
{
Count++;
}

if ((char)s[i] > 'A' && (char)s[i] < 'Z')
{

count++;
}

if ((int)s[i] > '0' && (int)s[i] < '9')
{

figure++;
}

else
{
other++;
}

}
Console.WriteLine("Count={0} ,count={1} ,fihure={2} ,other={3}",Count ,count ,figure ,other);
Console.ReadKey();
}

}
}


运行结果:



学习总结: 之前用C++语言写过这类题,所以换汤不换药,只是用另一种语言又写了一遍,熟练使用C#语言。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: