Write a program which given a string, swap first and last character of each word in it.
Complete the function swapFirstLastChar() that accepts a multiword string and swaps the first and last character of every word in it.
Note: Every two adjacent words in the given string will be separated by exactly one space character.
Input Format
The first lineof input consists numberof testcases, T
The next T lines contains a string on which the said operationis to be performed
Output Format
Foreach testcase,print the string after doing thegiven operatio
Sample Input
2
Code Quotient
Get better at coding
Sample Output
eodC tuotienQ
teG retteb ta godinc
class Result{
/*
* Complete the function capitalizeFirstChar
* @params
* str -> string which is to be modified
* @returns
* The modified string after performing the given operations
*/
static String swapFirstLastChar(String str) {
// Write your code here
StringBuilder result = new StringBuilder();
String[] words = str.split(" ");
for(String word: words)
{
result.append(word.charAt(word.length()-1))
.append(word.substring(1,(word.length()-1)))
.append(word.charAt(0))
.append(" ");
}
return result.toString().trim();
}
}