In: Computer Science
PLESE CODE IN C# not java
RECURSION
Objectives
• Learn the basics of recursion – Part II (last week was Part I) Submission Guidelines:
You will turn in 2 program files (one for each lab problem)
Tasks
This lab has two parts:
Write a driver program that calls this method from the Main program.
• •
Write a driver program that calls this method from the Main program.
Note: Your program name should match the name of your java / C# class exactly. e.g Lab10a.java / Lab10a.cs
Sample Output: LAB 1:
//A call to recursive function repeatNTimes(“hello”, 5) hellohellohellohellohello
// A call to recursive function repeatNTimes(“Good morning!”, 3) Good morning!Good morning!Good morning!
LAB 2:
1. Write a recursive method
repeatNTimes(String s, int n) that accepts a String and an
integer as two parameters and returns a string that is concatenated together n times. (For
example, repeatNTimes ("hello", 3) returns "hellohellohello")
2. 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.
Page 1 of 2
//Call to recursive function isReverse ("Computer","retupmCo") false
//Call to recursive function isReverse ("Hello","olleh") true
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. If not, PLEASE let me know before you rate, I’ll help you fix whatever issues. Thanks
/*Program 1*/
//Lab10a.cs
using System;
class Lab10a{
//method to return a string containing s repeated n times
public static String repeatNTimes(String s, int n){
//if n is 0 or below, returning empty string
if(n<=0){
return "";
}else{
//else returning s concatenated with the value returned from
//recursive call passing n-1 as new n
return s+repeatNTimes(s,n-1);
}
}
//Main method for testing
public static void Main(String[] args){
Console.WriteLine(repeatNTimes("hello", 5));
Console.WriteLine(repeatNTimes("Good morning!", 3));
//wait for a key press to quit. remove if not needed.
Console.ReadKey();
}
}
/*OUTPUT */
hellohellohellohellohello
Good morning!Good morning!Good morning!
/*Program 2*/
//Lab10b.cs
using System;
class Lab10a{
//method to check if s1 is same as s2, but in reverse
public static bool isReverse(String s1, String s2){
//if lengths of s1 an s2 mismatch, returning false
if(s1.Length!=s2.Length){
return false;
}
//if strings are empty, returning true
else if(s1.Length==0){
return true;
}
//comparing first char of s1 with last char of s2, after converting them to
//lower case
if(Char.ToLower(s1[0])!=Char.ToLower(s2[s2.Length-1])){
//mismatch s1 is not reverse of s2
return false;
}
//otherwise removing first char from s1, last char from s2, calling method
//recursively passing new strings
//note: s1.Substring(1) will return a s1 without first character
// s2.Substring(0,s2.Length-1) will return s2 without last character
return isReverse(s1.Substring(1),s2.Substring(0,s2.Length-1));
}
//Main method for testing
public static void Main(String[] args){
Console.WriteLine(isReverse ("Computer","retupmCo"));
Console.WriteLine( isReverse ("Hello","olleh") );
//wait for a key press to quit. remove if not needed.
Console.ReadKey();
}
}
/*OUTPUT*/
False
True