Given the string the task is to remove the first and last character of each word in a string.
Examples:
Input: Geeks for geeks Output: eek o eek Input: Geeksforgeeks is best Output: eeksforgeek es
Approach
Below is the implementation of the above approach.
// Java program to remove the first // and last character of each word in a string. import java.util.*; class GFG { static String FirstAndLast(String str) { // Split the String based on the space String[] arrOfStr = str.split(" "); // String to store the resultant String String res = ""; // Traverse the words and // remove the first and last letter for (String a : arrOfStr) { res += a.substring(1, a.length() - 1) + " "; } return res; } // Driver code public static void main(String args[]) { String str = "Geeks for Geeks"; System.out.println(str); System.out.println(FirstAndLast(str)); } }
Output:
Geeks for Geeks eek o eek
Australia
UK
UAE
Singapore
Canada
New
Zealand
Malaysia
USA
India
South
Africa
Ireland
Saudi
Arab
Qatar
Kuwait
Hongkong
Copyright 2016-2023 www.programmingshark.com - All Rights Reserved.
Disclaimer : Any type of help and guidance service given by us is just for reference purpose. We never ask any of our clients to submit our solution guide as it is, anywhere.