[英]How to move chars in char array to the left and to the right in c# sharp
guys!伙计们! I have this situation: Given a string and two more characters X and Y. Move all X characters to the beginning of the string and all Y characters to the end of the string.我有这种情况:给定一个字符串和另外两个字符 X 和 Y。将所有 X 字符移动到字符串的开头,将所有 Y 字符移动到字符串的末尾。 The order of the other characters in the string remains unchanged.字符串中其他字符的顺序保持不变。 I wrote two function MoveCharsLeft and MoveCharsRight to move X to the left and Y to the right, but there is an error System.IndexOutOfRangeException: 'Index was outside the bounds of the array.我写了两个函数MoveCharsLeft和MoveCharsRight来将 X 向左移动,Y 向右移动,但是有一个错误System.IndexOutOfRangeException: 'Index was outside the bounds of the array。 ' on this line of code char toReplace = splitText[charToCheck]; ' 在这行代码char toReplace = splitText[charToCheck]; and I don't know how to handle it, in order to solve the exercise.而我不知道如何处理它,以解决这个练习。 Can, you guys, help me with that, how should be the functions?你们能帮我解决这个问题吗,功能应该如何?
static void Main()
{
string text = Console.ReadLine();
char[] splitText = text.ToCharArray();
string firstLetter = Console.ReadLine();
char[] firstChar = firstLetter.ToCharArray();
string secondLetter = Console.ReadLine();
char[] secondChar = secondLetter.ToCharArray();
char one = firstChar[0];
char two = secondChar[0];
Console.WriteLine(CheckChars(splitText, one, two));
Console.ReadLine();
}
static char[] CheckChars(char[] splitText, char one, char two)
{
for (char letter = 'a'; letter <= 'z'; letter++)
{
if (Array.IndexOf(splitText, one) > -1)
{
MoveCharsLeft(splitText, one);
}
if (Array.IndexOf(splitText, two) > -1)
{
MoveCharsRight(splitText, two);
}
}
return splitText;
}
static void MoveCharsLeft(char[] splitText, char charToCheck)
{
char toReplace = splitText[charToCheck];
char currentLetter = splitText[0];
for (int i = 0; i <= charToCheck; i++)
{
char temporary = splitText[i];
splitText[i] = currentLetter;
currentLetter = temporary;
}
splitText[0] = toReplace;
}
static void MoveCharsRight(char[] splitText, char charToCheck)
{
char toReplace = splitText[charToCheck];
char currentLetter = splitText[-1];
for (int i = 0; i <= charToCheck; i++)
{
char temporary = splitText[i];
splitText[i] = currentLetter;
currentLetter = temporary;
}
splitText[-1] = toReplace;
}
You're not checking your boundaries.你没有检查你的界限。 I would say CheckChars
method is redundant and in MoveCharsLeft
and MoveCharsRight
you're not checking if you're still inside of splitText
.我会说CheckChars
方法是多余的,在MoveCharsLeft
和MoveCharsRight
您不会检查您是否仍在splitText
。
Here's how I would implement MoveCharsLeft
, I hope it'll guide you in the right direction.这是我将如何实现MoveCharsLeft
,我希望它会引导您朝着正确的方向前进。
char[] MoveCharactersLeft(char[] text, char character) {
var lowerCharacter = char.ToLower(character);
var left = new List<char>();
var right = new List<char>();
foreach (var letter in text) {
if (char.ToLower(letter) == lowerCharacter) {
left.Add(letter);
continue;
}
right.Add(letter);
}
left.AddRange(right);
return left.ToArray();
}
And here's MoveCharactersRight
:这是MoveCharactersRight
:
char[] MoveCharactersRight(char[] text, char character) {
var lowerCharacter = char.ToLower(character);
var left = new List<char>();
var right = new List<char>();
foreach (var letter in text) {
if (char.ToLower(letter) == lowerCharacter) {
right.Add(letter);
continue;
}
left.Add(letter);
}
left.AddRange(right);
return left.ToArray();
}
Now that you have both you simply call MoveCharsLeft
and then the MoveCharactersRight
on the result of MoveCharsLeft
.现在你有了这两个,你只需在MoveCharactersRight
的结果上调用MoveCharsLeft
和MoveCharsLeft
。
You can swap them in place, although it requires a lot of copying.您可以就地交换它们,尽管它需要大量复制。 Here's a function to move a character from one spot to another in an array.这是一个将字符从数组中的一个位置移动到另一个位置的函数。 You can find several examples of this on the web and SO .你可以在网上和SO上找到几个这样的例子。
public static void Move(char[] buffer, int from, int too)
{
if (from == too)
return;
char temp = buffer[from];
if (too < from)
{
Array.Copy(buffer, too, buffer, too + 1, from - too);
}
else
{
Array.Copy(buffer, from + 1, buffer, from, too - from);
}
buffer[too] = temp;
}
Then you just have to iterate through the array testing the characters, testing them and moving them to the right spot.然后你只需要遍历数组测试字符,测试它们并将它们移动到正确的位置。 Here's a function to do that:这是一个函数来做到这一点:
public static void ArrangeChars(char[] buffer, char left, char right)
{
int leftIndex = 0;
int rightIndex = buffer.Length - 1;
int i = 0;
while(i <= rightIndex)
{
char ch = buffer[i];
if (ch == left && i > leftIndex)
Move(buffer, i, leftIndex++);
else if (ch == right && i < rightIndex)
Move(buffer, i, rightIndex--);
else
++i;
}
}
Call it like so:像这样称呼它:
static void Main(string[] _)
{
char[] data = "test me and move".ToCharArray();
Console.WriteLine(new string(data));
ArrangeChars(data, 'e', 't');
Console.WriteLine("Move 'e' to start, move 't' to end");
Console.WriteLine(new string(data));
}
The usual caveats apply - this has not been well tested and if you use it, you should check all the edge cases.通常的警告适用 - 这还没有经过很好的测试,如果你使用它,你应该检查所有的边缘情况。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.