C#
输入一个由若干字符组成的字符串,输出其中的大写字母、小写字母、数字和其他字符的个数。
代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("请输入一串数字:");
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.Write("大写字母的个数为:");
Console.WriteLine(Count);
Console.Write("小写字母的个数为:");
Console.WriteLine(count);
Console.Write("数字的个数为:");
Console.WriteLine(figure);
Console.Write("其他字符的个数为:");
Console.WriteLine(other);
Console.ReadKey();
}
}
}
运行结果:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("请输入一串数字:");
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.Write("大写字母的个数为:");
Console.WriteLine(Count);
Console.Write("小写字母的个数为:");
Console.WriteLine(count);
Console.Write("数字的个数为:");
Console.WriteLine(figure);
Console.Write("其他字符的个数为:");
Console.WriteLine(other);
Console.ReadKey();
}
}
}