Using replacements and substring between variables, how can I go about completin
ID: 3585732 • Letter: U
Question
Using replacements and substring between variables, how can I go about completing this problem?
Specifically, how can I use the substring and index in order to substitute the instances of 'emu' with the full 'Eastern Michigan University'? How can I do it multiple times for the various instances?
And, given that I make a second string that turns everything to lower case for the replacement sake, how can I then put the changes back into the original string (Towards the bottom of the second picture, the statement used to test the program)?
This is using Java!
25%, 2:03 PM /r × lab6 DETAILS SUBMISSIONS GRADE Due: Oct 9, 2017 11:59 PM Last Submission: None This problem builds on the program you wrote last week that replaced all occurrences of "emu" ("EMU") with Easterrn Michigan University, and "Ypsi" with Ypsilanti For simplicity, instead of having the user type the sentence, create a String variable sentence to hold the sentence. Make your code generic enough that it will replace any of "emU", "EMu", "eMu", etc. with Eastern Michigan University. Similarly, your code should replace "Ypsi", "YpSi" yPsl', etc with Ypsilanti 1. Important: test your code by changing the sentence variable (try "ypSl "YpSl", and other combinations, same for "EmU", "eMu", etc) and make sure your code still replaces both strings correctly 2. Your program should replace allExplanation / Answer
Program:
package com.test;
import java.util.Scanner;
public class replaceString
{
public static void main(String args[])
{
/*
* Initialising Scanner to get the input from the user
*/
Scanner sc = new Scanner(System.in);
//Storing the value entered by user in variable "text"
String sentence = sc.nextLine();
//In replace all method if we give the target string in the form of regex(?i)
//it does'nt consider the case of the target substring
sentence = sentence.replaceAll("(?i)emu", "Eastern Michigan University");
sentence = sentence.replaceAll("(?i)ypsi", "Ypsilanti");
//printing the final value
System.out.println(sentence);
}
}
Output:
"Hello, I go to EmU in yPSi and both of my parents went to eMu and EMu has a beautiful campus, not to mention that eMU is ClosE to My Home in YPSi."
"Hello, I go to Eastern Michigan University in Ypsilanti and both of my parents went to Eastern Michigan University and Eastern Michigan University has a beautiful campus, not to mention that Eastern Michigan University is ClosE to My Home in Ypsilanti."
Kindly comment if you need anymore help with the program.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.