zoukankan      html  css  js  c++  java
  • 字符串反转的几种方法

    class Program
        {
            static void Main(string[] args)
            {
                string test = "this is test case";
                //except result="esac tset si siht";
                string result1 = Reserve1(test);
                string result2 = Reserve2(test);
                string result3 = Reserve3(test);
            }
    
            public static string Reserve1(string str)
            {
                string result = null;
                for (int i = str.Length - 1; i >= 0; i--)
                {
                    result += str[i];
                }
    
                return result;
            }
    
            public static string Reserve2(string str)
            {
                Hashtable hs = new Hashtable();
                int i = 0;
                foreach (char c in str)
                {
                    hs.Add(i, c);
                    ++i;
                }
    
                string result = null;
                foreach (DictionaryEntry c in hs)
                {
                    result += c.Value;
                }
    
                return result;
            }
    
            public static string Reserve3(string str)
            {
                Stack stack = new Stack();
                foreach (char c in str)
                {
                    stack.Push(c);
                }
                
                string result = null;
                foreach (var c in stack)
                {
                    result += c.ToString();
                }
    
                return result;
            }
    View Code
  • 相关阅读:
    bzoj2959
    学习笔记::lct
    bzoj3203
    bzoj1319
    bzoj3625
    bzoj3992
    bzoj1565
    bzoj3513
    平常练习动归(1.胖男孩)———最长公共子序列
    2016 noip 复赛 day2
  • 原文地址:https://www.cnblogs.com/binyao/p/4921718.html
Copyright © 2011-2022 走看看