Home Java Java-tips Data Strings Q-pr-string-if Programming - World Peace

Ask Questions?

View Latest Questions



Programming - World Peace
Posted on: July 26, 2006 at 12:00 AM
Change all occurences of "war" to "peace"

Java: Programming - World Peace

Change all occurences of "war" to "peace"

The US Department of Defense used to be called the War Department. I wouldn't be surprised if the public relations gurus eventually change it to Department of Peace.

In this same spirit you should write a program to produce world peace by scanning the input for the word "war" and replace it with "peace". The Java function replace may not be used for this -- you have to write it yourself.

Iterative development hints

  1. Write a program to just input a string.
  2. Use indexOf to tell whether it had the word "war" in it or not.
  3. Be sure your code works both when "war" occurs and when it doesn't.
  4. Use indexOf and substring to extract the part before "war" and after "war" and build a new string where "war" is replaced by "peace".
  5. Write a loop to replace ALL occurences of "war" with "peace".

Extra credit

Upper- and lowercase are problems. Add code to deal with case problems.