Reverse each word in sentence without changing position of word (Program_7 by School)
package School;
import java.util.Scanner;
import java.util.StringTokenizer;
public class Program_7 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Enter Sentence");
String sentence = in.nextLine();
sentence = sentence.toUpperCase();
String store="";
if (sentence.endsWith(".") || sentence.endsWith("!") || sentence.endsWith("?")){
sentence = sentence.substring(0, sentence.length() - 1);
StringTokenizer st = new StringTokenizer(sentence);
while (st.hasMoreTokens()){
store=st.nextToken();
for (int i=store.length()-1;i>=0;i--){
System.out.print(store.charAt(i));
}
System.out.print(" ");
}
System.out.println(".");
}
else
{
System.out.println("Invalid Input");
}
}
}
Comments
Post a Comment