• 8、颠倒任意一个字符串的X个字符(第一个和倒数第一个颠倒,第二个和倒数第二个颠倒 ... )


    #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
  • 相关阅读:
    vscode快捷键的中文版
    小程序css--view标签下英文不换行,中文会自动换行
    微信小程序设置背景铺满全屏
    MAC系统上不能调试华为手机
    js 空函数的作用
    五子棋 AI(AIpha-beta算法)
    ( function(){…} )()和( function (){…} () )是两种立即执行函数
    vscode快捷键
    20192328牛梓萌第一周作业
    第一次作业
  • 原文地址:https://www.cnblogs.com/LifeForCode/p/3360813.html
Copyright © 2020-2023  润新知