Back to course home
0% completed
Vote For New Content
My C# Solution
Amber Wolf
Apr 16, 2025
public bool IsPalindrome(string s) { // use Regex to easily remove special characters and spaces from our string var trimmedString = Regex.Replace(s, "[^a-zA-Z0-9]", "").ToLower(); // create an array from the string so we can iterate through it trimmedString.ToCharArray(); // we need to track both the Forward facing and Backward facing iterations of this array so I am initializing the index starting at the end of the array here. int j = trimmedString.Length - 1; // each time we loop through our array we will increase i and decrease j to compare both the forward and backward letter of the string. for (int i = 0; i < trimmedString.Length; i++, j--) { if (trimmedString[i] != trimmedString[j]) { // if the letters are ever not the same it means this is not a palindrome and we can end the loop/return false return false; } } // if we iterate through the entire string and each letter matches then we return true since it is a palindrome return true; }
0
0
Comments
Comments
On this page