Not only is this code not idiomatic C# (it seems to be written in another language), it does a lot of needless things, including memory allocation.
The main change is not to allocate a new object which is something that can create a change problem, I only made comparisons because the problem only asks for it. And comparing one by one is the best way. Although it is not visible these methods you are using are composed of loops, they scan the entire data every time, it is very bad.
Another change is to leave as soon as you know that it is not palindrome, and it should occur a lot, it is easy not to be. When you already know it’s not have to continue the algorithm.
Finally, I only check halfway, because if I continue the result is already known. What would I differ from analyzing the first with the last and then the last with the first? It is the same.
I didn’t improve the validation because I don’t know the requirements.
using static System.Console;
public class Program {
public static void Main() {
WriteLine(checkPalindrome("ana"));
WriteLine(checkPalindrome("abba"));
WriteLine(checkPalindrome("oki"));
}
static bool checkPalindrome(string inputString) {
if(inputString.Length >= 1 && inputString.Length <= 100_000) {
for (var i = 0; i < inputString.Length / 2; i++) if (char.ToLower(inputString[i]) != char.ToLower(inputString[inputString.Length - i - 1])) return false;
return true;
}
return false;
}
}
Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.
Maniero after seeing your code saw how much my do things without need rs.. Thanks for the help!
– rlimadev