ReverseString, a C# interview-question

前端 未结 12 1850
予麋鹿
予麋鹿 2021-01-31 06:15

I had an interview question that asked me for my \'feedback\' on a piece of code a junior programmer wrote. They hinted there may be a problem and said it will be used heavily o

12条回答
  •  深忆病人
    2021-01-31 07:08

    I prefer something like this:

    using System;
    using System.Text;
    namespace SpringTest3
    {
        static class Extentions
        {
            static private StringBuilder ReverseStringImpl(string s, int pos, StringBuilder sb)
            {
                return (s.Length <= --pos || pos < 0) ? sb : ReverseStringImpl(s, pos, sb.Append(s[pos]));
            }
    
            static public string Reverse(this string s)
            {
                return ReverseStringImpl(s, s.Length, new StringBuilder()).ToString();
            }
        }
    
        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine("abc".Reverse());
            }
        }
    }
    

提交回复
热议问题