ReverseSentence

ReverseSentence - for (int wordPlace = word.length() -1;...

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

View Full Document Right Arrow Icon
public class ReverseSentence { public static void main(String[] args) { String reversed = ""; System.out.println("Please enter a sentence:"); String line = IO.readString(); line = line + " "; // check for bad input. This si not needed for the practice exam // problem but was needed for the homework for(int place = 0; place<line.length(); place++){ if ("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ". indexOf(line.charAt(place)) == -1){ IO.reportBadInput(); } } while(line.length() > 0){ int spacePlace = line.indexOf(' '); String word = line.substring(0, spacePlace);
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: for (int wordPlace = word.length() -1; wordPlace &gt;=0; wordPlace--){ reversed = reversed + word.substring(wordPlace, wordPlace+1); } reversed = reversed + &quot; &quot;; line = line.substring(spacePlace+1); } // replace final space with a period reversed = reversed.substring(0,reversed.length()-1).concat(&quot;.&quot;); // capitalize String first = reversed.substring(0,1); reversed = first.toUpperCase().concat(reversed.substring(1) .toLowerCase()); IO.outputStringAnswer(reversed); } }...
View Full Document

This note was uploaded on 07/12/2009 for the course CS 111 taught by Professor Hari during the Fall '08 term at Rutgers.

Ask a homework question - tutors are online