In: Computer Science
Code in c#
Write a recursive method called isReverse(String s1, String s2) that accepts two strings as parameters and returns true if the two strings contain the same sequence of characters as each other but in the opposite order and false otherwise. • The recursive function should ignore capitalization. (For example, the call of isReverse("hello", "eLLoH") would return true.) • The empty string, as well as any one letter string, should be its own reverse. Write a driver program that calls this method from the Main program.
Program should ask for user input and reverse the sequence of characters.
C# Program:
using System;
// change class as you want
class HelloWorld {
static void Main() {
Console.WriteLine(isReverse("hello", "oLLeH"));
}
static bool isReverse(String s1, String s2)
{
// convert both string into lowercase to ignore capitalization
s1 = s1.ToLower();
s2 = s2.ToLower();
if (s1 == null || s2 == null) { // if one of the string is null
return false;
}
if (s1.Length == 1 && s2.Length == 1) { // if both string length is 1, then directly checks
return s1.Equals(s2);
}
else if (s1.Length == s2.Length) // else than above conditions
{
char let_s1 = s1[0]; // first character of string 1
char let_s2 = s2[s2.Length - 1]; // last character of string 2
if (let_s1 == let_s2) // if both charcters equal
{
// recursively call using inbuilt Substring method
// from second character of string 1 to second last character of string 2
// if equal return true
return isReverse(s1.Substring(1), s2.Substring(0, s2.Length - 1));
}
else
{
return false; // else false
}
}
return false; // if not satisfy all the above conditions then simply return false
}
}
Output:
Thumbs Up Please !!!