Remove the first and last character of each word in a string Last Updated : 25 Sep, 2022 Comments Improve Suggest changes Like Article Like Report Given the string the task is to remove the first and last character of each word in a string.Examples: Input: Geeks for geeksOutput: eek o eek Input: Geeksforgeeks is bestOutput: eeksforgeek es Approach : Split the String based on the spaceRun a loop from the first letter to the last letter.Check if the character is the starting or end of the wordRemove this character from the String. Below is the implementation of the above approach. C++ // C++ program to remove the first // and last character of each word in a string. #include<bits/stdc++.h> using namespace std; string FirstAndLast(string str) { // add a space to the end of the string str+=" "; string res="",w=""; // traverse the string and extract words for(int i=0;i<str.length();i++) { if(str[i]==' ') { // excluding the first and // last character res +=w.substr(1,w.length()-2)+" "; // clear the word w=""; } else { // else add the character to word w+=str[i]; } } return res; } // Driver code int main() { string str = "Geeks for Geeks"; cout << (str) << endl; cout << FirstAndLast(str) << endl; return 0; } // This code is contributed by Arnab Kundu Java // 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)); } } Python3 # Python3 program to remove the first # and last character of each word in a string. def FirstAndLast(string) : # Split the String based on the space arrOfStr = string.split(); # String to store the resultant String res = ""; # Traverse the words and # remove the first and last letter for a in arrOfStr : res += a[1:len(a) - 1] + " "; return res; # Driver code if __name__ == "__main__" : string = "Geeks for Geeks"; print(string); print(FirstAndLast(string)); # This code is contributed by Ryuga C# // C# program to remove the first // and last character of each word in a string. using System; 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 foreach (String a in arrOfStr) { res += a.Substring(1, a.Length-2) + " "; } return res; } // Driver code public static void Main(String []args) { String str = "Geeks for Geeks"; Console.WriteLine(str); Console.WriteLine(FirstAndLast(str)); } } /* This code contributed by PrinciRaj1992 */ PHP <?php // PHP program to remove the first // and last character of each word in a string. function FirstAndLast($str) { // add a space to the end of the string $str .=" "; $res = (string) NULL; $w = (string) NULL; // traverse the string and extract words for($i=0; $i< strlen($str); $i++) { if($str[$i]== ' ') { // excluding the first and // last character $res .=substr($w, 1 ,strlen($w)-2) ; $res .= " "; // clear the word $w= (string) NULL; } else { // else add the character to word $w .=$str[$i]; } } return $res; } // Driver code $str = "Geeks for Geeks"; echo $str , "\n"; echo FirstAndLast($str); // This code is contributed by ihritik ?> JavaScript <script> // JavaScript program to remove the first // and last character of each word in a string. function FirstAndLast(str) { // add a space to the end of the string str += " "; var res = "", w = ""; // traverse the string and extract words for (var i = 0; i < str.length; i++) { if (str[i] === " ") { // excluding the first and // last character res += w.substring(1, w.length - 1) + " "; // clear the word w = ""; } else { // else add the character to word w += str[i]; } } return res; } // Driver code var str = "Geeks for Geeks"; document.write(str + "<br>"); document.write(FirstAndLast(str) + "<br>"); </script> Output: Geeks for Geeks eek o eek Time complexity: O(n) where n is the length of the given stringAuxiliary space: O(n) because using extra space for string res and string w. Comment More infoAdvertise with us Next Article Remove the first and last character of each word in a string C code_r Follow Improve Article Tags : Strings DSA school-programming Practice Tags : Strings Similar Reads Print the first and last character of each word in a String Given a string s consisting of multiple words, print the first and last character of each word. If a word contains only one character, print it twice (since the first and last characters are the same).Note: The string will not contain leading or trailing spaces.Examples: Input: s = "Geeks for geeks" 5 min read Program for removing i-th character from a string Given a string S along with an integer i. Then your task is to remove ith character from S. Examples: Input: S = Hello World!, i = 7Output: Hello orld!Explanation: The Xth character is W and after removing it S becomes Hello orld! Input: S = GFG, i = 1Output: GGExplanation: It can be verified that a 5 min read Remove a Character from a Given Position Given a string and a position (0-based indexing), remove the character at the given position.Examples: Input : s = "abcde", pos = 1Output : s = "acde"Input : s = "a", pos = 0Output : s = ""Approach - By Using Built-In MethodsWe use erase in C++, string slicing in Python, StringBuilder Delete in Java 5 min read Remove last occurrence of a word from a given sentence string Given two strings S and W of sizes N and M respectively, the task is to remove the last occurrence of W from S. If there is no occurrence of W in S, print S as it is. Examples: Input: S = âThis is GeeksForGeeksâ, W="Geeks"Output: This is GeeksForExplanation:The last occurrence of âGeeksâ in the stri 11 min read Minimum operation require to make first and last character same Given a string S. You are allowed two types of operations: Remove a character from the front of the string.Remove a character from the end of the string. The task is to find the minimum operations required to make the first and last character of the S same. In case, it is not possible, print "-1". E 14 min read Like