Exchange the first and last letters of words in a sentence.
import java.util.*;
public class Exchange {
String sent, rev;
int size;
void input() {
Scanner sc = new Scanner(System.in);
System.out.print("Enter Sentence: ");
sent = sc.nextLine();
size = sent.length();
sc.close();
}
void exfirstlast() {
StringTokenizer st = new StringTokenizer(sent, " .");
StringBuffer str = new StringBuffer();
while (st.hasMoreTokens()) {
String s = st.nextToken();
str.append(exchange(s) + " ");
}
rev = str.toString();
}
void display() {
System.out.println("Sentence: " + sent);
System.out.println("Changed Sentence: " + rev);
}
private String exchange(String s) {
StringBuffer a = new StringBuffer(s);
a.setCharAt(0, s.charAt(s.length() - 1));
a.setCharAt(s.length() - 1, s.charAt(0));
return a.toString();
}
public static void main(String[] args) {
Exchange a = new Exchange();
a.input();
a.exfirstlast();
a.display();
}
}
Name | Type | Uses |
---|---|---|
global | ||
sent | String | to store the entered string |
rev | String | to store the reversed(word order) string required |
size | int | the size of the word entered |
void input() | ||
sc | Scanner | object to take user input |
void exfirstlast() | ||
st | StringTokenizer | object to extract tokens(words) from the sentence |
str | StringBuffer | to store the new resultant string |
String exchange() | ||
a | StringBuffer | to make and store the resultant string |
void main() | ||
a | Exchange | object to call its functions |