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) {
		Program10.frontBack("abcd");
		Program10.frontBack("Bee");
		Program10.frontBack("MazE");
	}
	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




Bushan Sirgur

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

This Post Has 2 Comments

  1. Chetan Bhandari

    Thanks, please create more question same concept to help,build logic

  2. SArvar

    you will enter three “a”.

Leave a Reply