Challenge: Write a Java program that asks the user to enter a phrase with a word repeated 3 times in it and a String to replace those repeated words with. An example of the program output:
Solution:
import java.util.*;
class lab3_1{
public static void main (String[]args)
{
String phrase;
String wordRepeated;
String secondWord;
Scanner in=new Scanner(System.in);
System.out.println("Enter a phrase with 3 words in it repeated: ");
phrase=in.nextLine();
System.out.println("Enter the word that is repeated: ");
wordRepeated=in.nextLine();
System.out.println("Enter a string: ");
secondWord=in.nextLine();
phrase=phrase.replaceAll(wordRepeated, secondWord);
System.out.println(phrase);
}
}
No comments:
Post a Comment