Getting Started: Click here for the starter code. As you work through the studio, you’ll be updating this code.
Reverse
Method¶The Reverse
method flips the order of the elements within an array.
However, Reverse
does not affect the digits or characters within those
elements.
Example
1 2 3 4 5 6 7 8 | string[] arr = new string[] {"hello", "world", "123", "orange"};
Array.Reverse(arr);
foreach(string a in arr)
{
Console.WriteLine(a);
}
|
Console Output
orange
123
world
hello
What if we wanted the reversed array to be
egnaro
321
dlrow
olleh
Let’s have some fun by creating a process that reverses BOTH the order of the entries in an array AND the order of characters within the individual elements.
Remember that a method should perform only one task.
ReverseNumbers
that will sort the numbers within an array in descending order.Sort
the array.Reverse
method to put the items into descending order.ReverseLetters
that will reverse each character inside a string and print the results to the console. This will be similar to the ReverseNumbers
method.C Sharp
, Hello
, Coding is fun!
they should return as prahS C
, olleH
, and !nuf si gniodC
.Now that you can completely reverse a string, let’s use this to check to see if a word is a palindrome.
A palindrome is a word that is spelled the same forwards and backwards. Some examples are sagas
, kayak
, and racecar
.
Note
There are other factors that are sometimes included in the definition of a palindrome. For example, an alternative definition is that a palindrome is a sentence or phrase that contains letters in the same order, whether considered from beginning-to-end, or end-to beginning, ignoring punctuation, case, and spaces.
One way to state a palindrome condition is to say that a palindrome is a string that is equal to its reverse. When we think about it that way, what we need to do is create a method that will take a string, reverse it, compare it to the original and verify if they are equal.
You have alredy built a function that reverses a string. It shouldn’t be too hard to extend that method to be able to detect palindromes.
IsPalindrome
, to check if a string is equal to its reverse. Think what data type the overall outcome will require.racecar
, civic
, sagas
.Bonus Mission: Modify your method to accept mulitple word palindromes. For example, Drab as a fool, aloof as a bard
or Never odd or even
.