(C#习题) 字符串
1.输入一个英文句子,翻转句子中单词的顺序,但单词内字符的顺序不变。句子中单词以空格符隔开。为简单起见,标点符号和普通字母一样处理。
例如输入“I am a student.”,则输出“student. a am I”。
namespace ConsoleApplication2 { class Program { // Use char array. static string Reverse1(string input) { char[] chars = input.ToCharArray(); char tmp; int indexStart = 0; int indexEnd = chars.Length - 1; // Reverse all chars. for (int i = indexStart, j = indexEnd; i < j; i++, j--) { tmp = chars[i]; chars[i] = chars[j]; chars[j] = tmp; } // Reverse sub strings split by blankspace. indexStart = 0; for (int k = 0; k < chars.Length; k++) { if (chars[k].Equals(' ')) { // Reverse substirngs. indexEnd = k - 1; for (int m = indexStart, n = indexEnd ; m<n; m++, n--) { tmp = chars[m]; chars[m] = chars [n]; chars[n] = tmp; } indexStart = k + 1; } } //Convert to string from char array. string reservedString = new string(chars); return reservedString; } // Use string method in C#. Split -> Inverted order output substrings. static string Reverse2(string input) { // Split by blankspace. string[] subStrings = input.Split(' '); StringBuilder stringBuilder = new StringBuilder(input.Length); // Order output subString; for (int i = subStrings.Length- 1; i>=0 ; i--) { if (i == 0) { stringBuilder.Append(subStrings[i]); } else { stringBuilder.Append(subStrings[i]).Append(' '); } } return stringBuilder.ToString(); } // Use string method in C#. Reverse -> Split -> Reverse static string Reverse3(string input) { char[] chars = input.ToCharArray(); Array.Reverse(chars); string reversedString = new string(chars); string[] subStrings = reversedString.Split(' '); StringBuilder stringBuilder = new StringBuilder(input.Length); for (int i = 0; i < subStrings.Length ; i++) { chars = subStrings[i].ToCharArray(); Array.Reverse(chars); reversedString = new string(chars); if (i == subStrings.Length - 1) { stringBuilder.Append(reversedString); } else { stringBuilder.Append(reversedString).Append(' '); } } return stringBuilder.ToString(); } // Use linq static string Reverse4(string input) { string output = new string(input.ToCharArray().Reverse().ToArray()); string[] subStrings = output.Split(' '); StringBuilder stringBuilder = new StringBuilder(input.Length); string reversedString; for (int i = 0; i < subStrings.Length; i++) { reversedString = new string(subStrings[i].ToCharArray().Reverse().ToArray()); if (i == subStrings.Length -1) { stringBuilder.Append(reversedString); } else { stringBuilder.Append(reversedString).Append(' '); } } return stringBuilder.ToString(); } static void Main(string[] args) { string myString = "I am a student."; Console.WriteLine("Reversed sentence is: {0}", Reverse1(myString)); Console.WriteLine("Reversed sentence is: {0}", Reverse2(myString)); Console.WriteLine("Reversed sentence is: {0}", Reverse3(myString)); Console.WriteLine("Reversed sentence is: {0}", Reverse4(myString)); Console.ReadLine(); } } }
题目:在一个字符串中找到第一个只出现一次的字符。如输入abaccdeff,则输出b。
namespace ConsoleApplication2 { class Program { // Search in array . o(n2) static char FirstShowOneTimeCharacter1(string input) { int[] showTimes = new int[input.Length]; char[] chars = input.ToCharArray(); int index = 0; for (;index < input.Length; index++) { for (int i = 0; i < input.Length; i++) { if (chars[index].Equals(chars[i])) { showTimes[index]++; } } if (showTimes[index] == 1) { break; } } return chars[index]; } // Use hash table. O(n2) . It is wrong here. static char FirstShowOneTimeCharacter2(string input) { Hashtable hashtable = new Hashtable(256); //Char : 1 byte. char[] chars = input.ToCharArray(); for (int i = 0; i < input.Length; i++) { if (hashtable.ContainsKey(chars[i])) { hashtable[chars[i]] = int.Parse(hashtable[chars[i]].ToString()) + 1; } else { hashtable.Add(chars[i], 1); } } // It is reversed output when the result is output. foreach (DictionaryEntry de in hashtable) { if (de.Value.ToString().Equals("1")) { return (char)de.Key; } } return '0'; } // Use bitMap. O(n) , array[asciiCode] = showTimes static char FirstShowOneTimeCharacter3(string input) { int[] bitMap = new int[256]; // Set initial show times to zero. for (int i = 0; i < bitMap.Length; i++) { bitMap[i] = 0; } // Get all characters' show times. char[] chars = input.ToCharArray(); for (int i = 0; i < chars.Length; i++) { // Use (int) to transfer character to its ASCII code. bitMap[(int)chars[i]]++; } // Get the first character which shows only one time. for (int asciiCode = 0; asciiCode < bitMap.Length; asciiCode++) { if (bitMap[asciiCode] == 1) { return (char)asciiCode; } } return '0'; } // Use linq O(?) static char FirstShowOneTimeCharacter4(string input) { var result = (from item in input group item by item into groups where groups.Count() == 1 select groups).First(); return result.Key; } static void Main(string[] args) { string myString = "abaccdeff"; Console.WriteLine("First show 1 time's character is: {0}", FirstShowOneTimeCharacter1(myString)); // b Console.WriteLine("First show 1 time's character is: {0}", FirstShowOneTimeCharacter2(myString)); // e Console.WriteLine("First show 1 time's character is: {0}", FirstShowOneTimeCharacter3(myString)); // b Console.WriteLine("First show 1 time's character is: {0}", FirstShowOneTimeCharacter4(myString)); // b Console.ReadLine(); } } }