Implement Replace from Chapter 6. Use a Separate Class for Replace Class. Add a
ID: 3884234 • Letter: I
Question
Implement Replace from Chapter 6. Use a Separate Class for Replace Class. Add a Method that will convert a String to Upper case. Add an Initial Menu, "1- Search and Replace" "2- Convert to Upper Case" "3-Exit" Prompt for the initial target strings for the two cases and return the answer to the console. Continue looping until exit is selected. public class Replace { private String myString: public Replace(String s) { myString = s: } public String MyReplace(String oldWord, String newWord) { String temp = myString: int position = temp.IndexOf(oldword): while (position != -1) { temp = temp.Substring(0, position) + newword + temp.Substring(position + oldWord.Length): position = temp.IndexOf(oldWord, position + newWord.Length): } return temp: public static void Main() { Console.Write("Enter a String which includes "fish"": ""): Replace phrase = new Replace(Console.ReadLine()): String changed = phrase.MyReplace(""fish""Explanation / Answer
using System;
public class Test
{
class Replace
{
private String myString;
public Replace(String s)
{
myString=s;
}
public String MyReplace(String oldWord, String newWord)
{
String temp=myString;
int position = temp.IndexOf(oldWord);
while(position!=-1)
{
temp=temp.Substring(0,position)+newWord + temp.Substring(position+oldWord.Length);
position = temp.IndexOf(oldWord, position+newWord.Length);
}
return temp;
}
public String convertToUpperCase()
{
return myString.ToUpper();
}
}
public static void Main()
{
Console.WriteLine("Enter any string ");
Replace phrase = new Replace(Console.ReadLine());
int ch=0;
while(ch!=3)
{
Console.WriteLine("Enter 1 to Search and Replace 2 to convert to Upper case 3 to Exit");
ch= Convert.ToInt32(Console.ReadLine());
if(ch==1)
{
String s,r;
Console.WriteLine("Enter search string ");
s=Console.ReadLine();
Console.WriteLine("Enter replace string ");
r=Console.ReadLine();
Console.WriteLine("New String is "+ phrase.MyReplace(s, r));
}
else if(ch==2)
Console.WriteLine("Upper Case: "+phrase.convertToUpperCase());
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.