一道C#字符串题将字符串"ABCDEGFHIJK"相邻的2个字母如"AB"调转为"BA"直到最后!

来源:百度知道 编辑:UC知道 时间:2024/06/22 20:17:15
一道C#字符串题将字符串"ABCDEGFHIJK"相邻的2个字母如"AB"调转为"BA"还需要在B的后面加个随机数字如 "B123A" 直到最后!

Console.WriteLine("请输入一个字符串:");
string str = Console.ReadLine();

if (str.Length > 1)
{
string result = string.Empty;

for (int i = 0; i < str.Length; i += 2)
{
Random rnd = new Random();
result += str[i + 1] + rnd.Next(999999).ToString() + str[i];
}

Console.WriteLine("反转后的结果是:" + result);
Console.ReadKey(true);
}

换完以后A在最后,其他顺序不变,也就是在BCDEFGHIJKA之间插入随机数...

按你的要求
StringBuilder str = new StringBuilder("ABCDEFGHIJK");
StringBuilder newStr = new StringBuilder();
Random ran = new Random();
int i = 0;
for (; i < str.Length-1; i++)
{
char temp;
temp =