Remove the first and last character of each word in a string

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

  • Split the String based on the space
  • Run a loop from the first letter to the last letter.
  • Check if the character is the starting or end of the word
  • Remove this character from the String.

Below is the implementation of the above approach.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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));
    }
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 */

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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
  
?>

chevron_right


Output:

Geeks for Geeks
eek o eek


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.





Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.