.NET实验6-4

来源:互联网 发布:手机音频合成软件 编辑:程序博客网 时间:2024/04/30 02:15
定义一个静态成员方法,该方法实现字符串反转。如Reverse("6221982")返回值为2891226.
public static string Reverse(string str)
{
    //方法主体中使用StringBuilder

}

提示:
1、将string转化成char
 char[] c = str.ToCharArray();
2、使用StringBuilder类

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace ConsoleApplication3{    class Program    {        static void Main(string[] args)        {            Console.WriteLine(Reverse("6221982"));            Console.ReadKey();              }        public static string Reverse(string str)        {            StringBuilder strR = new StringBuilder();            char[] c = str.ToCharArray();   //将字符串分离成字符数组            for (int i = c.Length - 1; i >= 0; --i)            {                strR.Append(c[i]);          //向实例的尾端追加字符串            }            return strR.ToString();        }    }}

运行结果:



0 0