I made this program which shall turn around sentences like: User Input: Hello i´m hungry. Where is the fridge. System Output: Hungry i´m Hello. Fridge the is Where.
But there is space between the last word and "." in the reversed sentence. How can i remove? And how can i make the first word a uppercase word?
package etzale;
public class etzale {
public static void main(String[] args) {
StringBuilder outputString= new StringBuilder();
String satz;
System.out.print("Bitte geben Sie einen String ein: ");
String text="Hallo mir gehts gut. Wie gehts dir. mir gehts spitze.";
while(text.indexOf(".")>=0){
satz=text.substring(0, text.indexOf("."));
text=text.substring(text.indexOf(".")+1);
String []s= satz.split(" ");
for(int i=s.length-1; i>=0; i--){
outputString.append(s[i]);
if(s[0]==" ");
outputString.append(" ");
}
outputString.append(".");
outputString.append(" ");
}
System.out.print(outputString);
}
}
How can i erase the space between the last word and "." in each sentence?
Actual Input: Mit gehts gut. Wie gehts dir. Mir gehts spitze.
Actual Output: gut gehts mir . dir gehts Wie . spitze gehts Mir .