{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

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 >=0; wordPlace--){ reversed = reversed + word.substring(wordPlace, wordPlace+1); } reversed = reversed + " "; line = line.substring(spacePlace+1); } // replace final space with a period reversed = reversed.substring(0,reversed.length()-1).concat("."); // capitalize String first = reversed.substring(0,1); reversed = first.toUpperCase().concat(reversed.substring(1) .toLowerCase()); IO.outputStringAnswer(reversed); } }...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online