The code you provided runs fine, but you can improve it. At the moment you create numerous new string
instances each time you add character, because string
s are immutable.
s2 += s1[i];
These allocations are expensive. You can check the string
by comparing the first with the last character, then the second with the second last, or in general index i
with text.Length - i - 1
:
private bool IsPalindrome(string text)
{
for (var i = 0; i < text.Length / 2; i++)
{
if (text[i] != text[text.Length - i - 1])
return false;
}
return true;
}