Skip to content
Related Articles
Get the best out of our app
GFG App
Open App
geeksforgeeks
Browser
Continue

Related Articles

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

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

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




// 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 string
Auxiliary space: O(n) because using extra space for string res and string w.


My Personal Notes arrow_drop_up
Last Updated : 25 Sep, 2022
Like Article
Save Article
Similar Reads
Related Tutorials