S
Simona.M
Guest
Hello everyone!
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. 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.' on this line of code char toReplace = splitText[charToCheck]; line 41 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?
using System;
namespace StringExercise
{
class Program
{
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;
splitText = 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;
splitText = currentLetter;
currentLetter = temporary;
}
splitText[-1] = toReplace;
}
}
}
Continue reading...
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. 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.' on this line of code char toReplace = splitText[charToCheck]; line 41 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?
using System;
namespace StringExercise
{
class Program
{
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;
splitText = 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;
splitText = currentLetter;
currentLetter = temporary;
}
splitText[-1] = toReplace;
}
}
}
Continue reading...