Replace - phrase2.indexOf(" " +...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
public class Replace { public static void main(String[] args) { System.out.print("Enter a phrase: "); String phrase = IO.readString(); System.out.print("Replace what: "); String word1 = IO.readString(); System.out.print("With what: "); String word2 = IO.readString(); if (word1.equals(word2)) { System.out.println(phrase); return; } String phrase2 = " " + phrase.toLowerCase() + " "; phrase = " " + phrase + " "; p while (phrase2.indexOf(" " + word1.toLowerCase() + " ") >= 0 &&
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: phrase2.indexOf(" " + word1.toLowerCase() + " ") < phrase.length()) { int index = phrase2.indexOf(" " + word1 + " "); String before; if (index > 0) before = phrase.substring(0, index); else before = ""; String after = phrase.substring(index + word1.length() + 1,phrase.length()); phrase2 = before.toLowerCase() + " " + word2 + after.toLowerCase(); phrase = before + " " + word2 + after; } System.out.println(phrase.trim()); } }...
View Full Document

Ask a homework question - tutors are online