Given a string of even length, return the first half. So the string “WooHoo” yields “Woo”.





Following are the test cases

Test cases Result Status
firstHalf(HelloThere) Hello OK
firstHalf(abcdef) abc OK
firstHalf(Word) Wo OK

Java solution


package test;

public class Program13 {
	public static void main(String[] args) {
		Program13.firstHalf("HelloThere");
		Program13.firstHalf("abcdef");
		Program13.firstHalf("Word");
	}
	public static String firstHalf(String str) {
		return str.substring(0, str.length() / 2);
	}

}

Javascript solution


const firstHalf = (str) => {
    return str.substr(0, str.length/2);
}

console.log(firstHalf("HelloThere")); //Hello
console.log(firstHalf("abcdef")); //abc
console.log(firstHalf("Word")); //Wo




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 *