Remove the first and last occurrence of a given Character from a String

Given a character C and a string S, the task is to remove the first and last occurrence of the character C from the string S.
Examples:

Input: S = “GeekforGeeks”, C = ‘e’ 
Output: GeksforGeks 
Explanation: 
GeekforGeeks -> GekforGeks

Input: S = “helloWorld”, C = ‘l’ 
Output: heloWord

Approach: 
The idea is to traverse the given string from both ends and find the first occurrences of the character C encountered and remove the corresponding occurrences. Finally, print the resultant string.

Below is the implementation of the above approach:



C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ Program to implement
// the above approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to remove first and last
// occurrence of a given character
// from the given string
string removeOcc(string& s, char ch)
{
    // Traverse the given string from
    // the beginning
    for (int i = 0; s[i]; i++) {
  
        // If ch is found
        if (s[i] == ch) {
            s.erase(s.begin() + i);
            break;
        }
    }
  
    // Traverse the given string from
    // the end
    for (int i = s.length() - 1;
        i > -1; i--) {
  
        // If ch is found
        if (s[i] == ch) {
            s.erase(s.begin() + i);
            break;
        }
    }
  
    return s;
}
  
// Driver Code
int main()
{
    string s = "hello world";
  
    char ch = 'l';
  
    cout << removeOcc(s, ch);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program to implement
// the above approach
class GFG{
  
// Function to remove first and last
// occurrence of a given character
// from the given String
static String removeOcc(String s, char ch)
{
    // Traverse the given String from
    // the beginning
    for (int i = 0; i < s.length(); i++) 
    {
  
        // If ch is found
        if (s.charAt(i) == ch) 
        {
            s = s.substring(0, i) + 
                s.substring(i + 1);
            break;
        }
    }
  
    // Traverse the given String from
    // the end
    for (int i = s.length() - 1; i > -1; i--)
    {
  
        // If ch is found
        if (s.charAt(i) == ch) 
        {
            s = s.substring(0, i) + 
                s.substring(i + 1);
            break;
        }
    }
    return s;
}
  
// Driver Code
public static void main(String[] args)
{
    String s = "hello world";
  
    char ch = 'l';
  
    System.out.print(removeOcc(s, ch));
}
}
  
// This code is contributed by sapnasingh4991

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to implement
# the above approach
  
# Function to remove first and last
# occurrence of a given character
# from the given string
def removeOcc(s, ch):
      
    # Traverse the given string from
    # the beginning
    for i in range(len(s)):
  
        # If ch is found
        if (s[i] == ch):
            s = s[0 : i] + s[i + 1:]
            break
      
    # Traverse the given string from
    # the end
    for i in range(len(s) - 1, -1, -1):
  
        # If ch is found
        if (s[i] == ch):
            s = s[0 : i] + s[i + 1:]
            break
  
    return s
  
# Driver Code
s = "hello world"
  
ch = 'l'
  
print(removeOcc(s, ch))
  
# This code is contributed by sanjoy_62

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# Program to implement
// the above approach
using System;
class GFG{
  
// Function to remove first and last
// occurrence of a given character
// from the given String
static String removeOcc(String s, char ch)
{
    // Traverse the given String from
    // the beginning
    for (int i = 0; i < s.Length; i++) 
    {
  
        // If ch is found
        if (s[i] == ch) 
        {
            s = s.Substring(0, i) + 
                s.Substring(i + 1);
            break;
        }
    }
  
    // Traverse the given String from
    // the end
    for (int i = s.Length - 1; i > -1; i--)
    {
  
        // If ch is found
        if (s[i] == ch) 
        {
            s = s.Substring(0, i) + 
                s.Substring(i + 1);
            break;
        }
    }
    return s;
}
  
// Driver Code
public static void Main(String[] args)
{
    String s = "hello world";
  
    char ch = 'l';
  
    Console.Write(removeOcc(s, ch));
}
}
  
// This code is contributed by sapnasingh4991

chevron_right


Output: 

helo word

Time Complexity: O(N) 
Auxiliary Space: O(1)
 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




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.



Improved By : sapnasingh4991, sanjoy_62