#region //8、颠倒任意一个字符串的X个字符(第一个和倒数第一个颠倒,第二个和倒数第二个颠倒 ... ) //8、颠倒任意一个字符串的X个字符(第一个和倒数第一个颠倒,第二个和倒数第二个颠倒 ... ) Console.WriteLine("请输入要颠倒的字符串:"); string repeatStr = Console.ReadLine(); int number = 0; while (true) { Console.WriteLine("请输入颠倒几对字符串({0}和{1}之间的偶数个):", 1, repeatStr.Length / 2); number = int.Parse(Console.ReadLine()); //保证输入的是偶数 if (number % 2 == 0) { //限制范围 if (number >= 1 && number <= repeatStr.Length) { break; } } } Console.WriteLine("颠倒前:"); Console.WriteLine(repeatStr); List<string> repeatList = new List<string>(); for (int j = 0; j < repeatStr.Length; j++) { repeatList.Add(repeatStr[j].ToString()); } string item = ""; for (int i = 0; i < number / 2; i++) { //这里默认从下标为0的开始 item = repeatList[i]; repeatList[i] = repeatList[repeatList.Count - 1 - i]; repeatList[repeatList.Count - 1 - i] = item; } //string[] repeatStr2=repeatList.ToArray(); char[] repeatStr2 = new char[repeatList.Count]; repeatStr = ""; for (int k = 0; k < repeatList.Count; k++) { repeatStr += repeatList[k]; } Console.WriteLine("颠倒后:"); Console.WriteLine(repeatStr); Console.ReadLine(); #endregion