第一次面试时出的题,想起来了写了一遍,留个念想.
输入一串字符串,计算大写字母个数.
static void Main(string[] args)
{
Console.Write("请输入大小写字母,以回车键结束:");
string inputStr = Console.ReadLine();
char[] CharArr = inputStr.ToCharArray();
int count=0;
for (int i = 0; i <CharArr.Length; i++)
{
if (CharArr[i]>='A' && CharArr[i]<='Z')
count++;
}
Console.WriteLine("搜索到大写字母:{0}个",count);
Console.ReadLine();
}
{
Console.Write("请输入大小写字母,以回车键结束:");
string inputStr = Console.ReadLine();
char[] CharArr = inputStr.ToCharArray();
int count=0;
for (int i = 0; i <CharArr.Length; i++)
{
if (CharArr[i]>='A' && CharArr[i]<='Z')
count++;
}
Console.WriteLine("搜索到大写字母:{0}个",count);
Console.ReadLine();
}
static void Main(string[] args)
{
Console.Write("请输入大小写字母,以回车键结束:");
int count = 0;
foreach (char c in Console.ReadLine().ToCharArray())
{
if (c >= 'A' && c <= 'Z')
count++;
}
Console.WriteLine("搜索到大写字母:{0}个", count);
Console.ReadLine();
}
{
Console.Write("请输入大小写字母,以回车键结束:");
int count = 0;
foreach (char c in Console.ReadLine().ToCharArray())
{
if (c >= 'A' && c <= 'Z')
count++;
}
Console.WriteLine("搜索到大写字母:{0}个", count);
Console.ReadLine();
}
static void Main(string[] args)
{
Console.Write("请输入大小写字母,以回车键结束:");
Console.WriteLine ("搜索到大写字母:{0}个",
Console.ReadLine().ToCharArray().
Where(c => c >= 'A' && c<='Z').Count().ToString());
Console.ReadLine();
}
{
Console.Write("请输入大小写字母,以回车键结束:");
Console.WriteLine ("搜索到大写字母:{0}个",
Console.ReadLine().ToCharArray().
Where(c => c >= 'A' && c<='Z').Count().ToString());
Console.ReadLine();
}