(not allowed to use LINQ or other libraries) I've created a While loop to convert each char into it opposite form of Uppercase or Lowercase and at the end of the loop I'm getting a 'System.IndexOutOfRangeException: 'Index was outside the bounds of the array.'' and I've no idea how to resolve the error. The loop carries out as expected but faces this error when the last char in the array has been converted and retests the parameters of the 1st embedded while loop. Also if there's an easier way to do this I'm all ears. Thanks in advance
string text2 = "HELLO my friEND";
char[] convert = text2.ToCharArray();
int b = 0;
int a = convert.Length;
while (b <= a)
{
while (char.IsUpper(convert[b]))
{
char.ToLower(convert[b]);
b ;
}
while (char.IsLower(convert[b]))
{
char.ToLower(convert[b]);
b ;
}
while (char.IsWhiteSpace(convert[b]))
{
b ;
}
}
string hello = new string(convert);
Console.Write(hello);
Console.WriteLine();
Console.Write("test");
return 0;
CodePudding user response:
while (b <= a)
needs to be while (b < a)
. Indexes are zero-based, meaning the last index will be equal to length - 1
.
You also increment b
after every check (the b
in each of the internal while
loops) - once the last letter is converted it checks the condition in the while
loop again, and after it converted the "D" b
is now 15 - but the string is only length 14. You need to check for this in each of the internal while
loops, something like
while (b < a && char.IsUpper(convert[b]))
Alternatively, rework this method entirely to avoid these kinds of problems in the first place (by e.g., using a for
loop).
CodePudding user response:
You had a lot of problems with your code, you weren't actually mutating the array by using the return result of the char methods, you had 2 ToLowers()
and you were reading over the end of the array.
However, you can make it a little more succinct
string text2 = "HELLO my friEND";
char[] convert = text2.ToCharArray();
for (int i = 0; i < convert.Length; i )
if (char.IsWhiteSpace(convert[i]))
continue;
else if (char.IsUpper(convert[i]))
convert[i] = char.ToLower(convert[i]);
else
convert[i] = char.ToUpper(convert[i]);
Purely academic purposes, you could also use a switch here, though its is the wrong tool for the job
for (int i = 0; i < convert.Length; i )
convert[i] = convert[i] switch
{
_ when char.IsWhiteSpace(convert[i]) => convert[i],
_ when char.IsUpper(convert[i]) => char.ToLower(convert[i]),
_ when char.IsLower(convert[i]) => char.ToUpper(convert[i]),
_ => throw new ArgumentOutOfRangeException()
};
And as a bonus prize, here is a linq variation
string text2 = "HELLO my friEND";
static char Change(char c)
{
if (char.IsWhiteSpace(c)) return c;
return char.IsUpper(c) ? char.ToLower(c) : char.ToUpper(c);
}
Console.Write(string.Concat(text2.Select(Change)));
CodePudding user response:
Please optimize your code like this:
while (b < a)
{
if (char.IsUpper(convert[b]))
{
convert[b] = char.ToLower(convert[b]);
}
else if (char.IsLower(convert[b]))
{
convert[b] = char.ToUpper(convert[b]);
}
b ;
}