Given a string, return a new string where the first and last chars have been exchanged

Test cases

Test cases Expected Result
frontBack(abcd) dbca OK
frontBack(Bee) eeB OK
frontBack(MazE) EazM OK

Java solution:

public class Program10 {
	public static void main(String[] args) {
	public static String frontBack(String str) {
		return str.substring(str.length()-1) + str.substring(1,str.length()-1) + str.charAt(0);

Javascript solution:

const frontBack = (str) => {
    return str[str.length-1] + str.substring(1, str.length-1) + str[0];

console.log(frontBack("abcd")); //dbca
console.log(frontBack("Bee")); //eeB
console.log(frontBack("MazE")); //EazM

About the author

Bushan Sirgur

Hey guys, I am Bushan Sirgur from Banglore, India. Currently, I am working as an Associate project in an IT company.

View all posts


Leave a Reply

Your email address will not be published. Required fields are marked *