一个c#面试题
C#
给你一个string字符串, abcd as,ccd.dsa
反转这个字符串变为: dsa.ccd,as abcd
要求:不能用【任何】【系统函数】。
实现代码如下:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions; namespace ReverseString { class Program { public static string ReverseString(String originalString) { if (string.IsNullOrWhiteSpace(originalString)) { throw new ArgumentException("orginalString can not be null or white space"); } char[] temp = originalString.ToCharArray(); int begin = 0, end = temp.Length - 1; ReverseCharArray(temp, begin, end); Console.WriteLine(temp); int i = 0, j = 0; int k = 0; do { if (!char.IsLetter(temp[k])) { j = k - 1; ReverseCharArray(temp, i, j); i = k + 1; } k++; } while (k <= end); if (char.IsLetter(temp[end])) { ReverseCharArray(temp, i, end); } string finalString = new string(temp); return finalString; } public static void ReverseCharArray(char[] charArray, int begin, int end) { while (begin < end) { char temp = charArray[begin]; charArray[begin] = charArray[end]; charArray[end] = temp; begin++; end--; } } static void Main(string[] args) { String originalString = "abcd as,ccd.dsa"; String ouput = "dsa.ccd,as abcd"; ReverseString(originalString); Console.ReadKey(); } } }
即使是骗,也要勤学苦练。