字符串颠倒并输出

编程入门 行业动态 更新时间:2024-10-11 23:16:51

<a href=https://www.elefans.com/category/jswz/34/1771434.html style=字符串颠倒并输出"/>

字符串颠倒并输出

            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();

更多推荐

字符串颠倒并输出

本文发布于:2024-02-07 08:39:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1755705.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!