字符串颠倒并输出"/>
字符串颠倒并输出
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("请输入字符串:");
Console.ForegroundColor = ConsoleColor.Yellow;
string s = Console.ReadLine();
string[] a = s.Split(' ');
Array.Reverse(a);
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("倒序以后是:");
for(int i=0;i<a.Length;i++)
{
Console.WriteLine(a[i] + "" + ' ');
}
Console.ReadKey();
第二种方法
Console.WriteLine("请输入字符串");
string s = Console.ReadLine();
char[] c = s.ToCharArray();//把接收到的字符串转成char类型数组
Array.Reverse(c, 0, c.Length);//使用Array类的Recerse方法实现颠倒
StringBuilder str2 = new StringBuilder();//创建StringBuilder对象
str2.Append(c).ToString();//获取指定数组,然后转换成字符串
Console.WriteLine(str2);//输出颠倒后的结果
Console.ReadKey();
更多推荐
字符串颠倒并输出
发布评论