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




Our Best Partners: wasamba 1win demo pay n play vedonlyönti suomalaiset kasinot ilman rekisteröitymistä wazamba parhaat nettikasinot ilman rekisteröitymistä play n go kasinot slots demo pg viron lisenssi kasino bingo bongo casino

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