字符串里面的单词反转,可以用string/stringBuilder

View Code
using System;
using System.Collections;
using System.Text;

namespace InsertionSorter
{
    public static class InsertionSorter
    {
        public static string ReverseString(string str)
        {
            string result = null;
            string[] s = str.Split(' ');
            for (int i = 0; i < s.Length; i++)
            {
                for (int j = s[i].Length - 1; j >= 0; j--)
                {
                    result += (s[i])[j];
                }

                result += " ";
            }

            return result;
            //Result: siht si a .tset 
        }
        public static string StrReverse(string str)
        {
            StringBuilder sb = new StringBuilder();
            string[] s = str.Split(' ');
            for (int i = 0; i < s.Length; i++)
            {
                sb.Append(Reverse(s[i]) + " ");
            }

            return sb.ToString();
            //Result: siht si a .tset 
        }
        private static string Reverse(string str)
        {
            StringBuilder sb = new StringBuilder();
            for (int i = str.Length - 1; i >= 0; i--)
            {
                sb.Append(str[i]);
            }

            return sb.ToString();
        }
    }
    public class MainClass
    {
        public static void Main()
        {
            string result = null;
            string thisString = "this is a test.";
            InsertionSorter.ReverseString(thisString);
            result = InsertionSorter.StrReverse(thisString);

            Console.WriteLine(result);

        }
    }
}

 

相关文章:

  • 2021-08-04
  • 2021-12-05
  • 2022-12-23
  • 2021-07-24
  • 2022-12-23
  • 2021-11-21
  • 2021-07-10
猜你喜欢
  • 2021-07-25
  • 2021-08-21
  • 2021-06-08
  • 2021-05-15
  • 2022-02-21
相关资源
相似解决方案