c#编程:接收键盘输入的英文字符串(单词之间用空格隔开),将字符串中出现的单词首字母改成大写后输出

2024-11-09 04:35:55
推荐回答(1个)
回答(1):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{

string [] words = null;
StringBuilder strBuff = null;
while (true)
{
try
{
System.Console.WriteLine("请输入英文字符串!(单词间用空格分隔)");
words = System.Console.ReadLine().Split(new char[] { ' ' });
if (words.Length > 0)
{
strBuff = new StringBuilder();
strBuff.Append(words[0].ToLower());
for (int i = 1; i < words.Length; i++)
{
words[i] = words[i].ToLower();
strBuff.AppendFormat("{0}{1}", Char.ToUpper(words[i][0]), words[i].Substring(1));
}
System.Console.WriteLine("结果:{0}", strBuff.ToString());
break;
}
}
catch
{
System.Console.WriteLine("输入不正确!请重新输入");
}
}
System.Console.Read(); //按回车结束程序
}
}
}