Ad space (banner)
#๏ธโƒฃC# Lessons
Lesson 43 / 68

Reversing a String and Checking for Palindromes

In this lesson you will learn how to reverse a string in C# and check whether it is a palindrome, covering the basics of string manipulation. This is for anyone searching for "C# reverse string palindrome check".

.Reverse() flips the order of the characters. A string that reads the same forwards and backwards is called a palindrome.

The sample code reverses the characters with s.Reverse().ToArray() inside IsPalindrome(), turns them back into a string with new string(...), and compares with the original s. "racecar" comes out the same either way, so it is reported as a palindrome.

A common stumbling block for beginners is that .Reverse() returns a sequence of characters. You need the new string() constructor to turn it back into a string, and forgetting that gives a compile error.

This is one of the simple, readable idioms you get from C# treating a string as a sequence of characters. StringBuilder is available too when you need to concatenate large amounts of text efficiently.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run C# directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)