Q. Write a program to remove all occurrences of a character in a string.

We are provided a string and a character in input and we will remove each occurrence of that character from the string and output the updated string.

String :- In computer programming, a string is traditionally a sequence of characters, either as a literal constant or as some kind of variable.
For Example :-
String = Letsfindcourse
Character = e Output = Ltsfindcours

Logic : We have a string and a character to be removed. Now we have passed these two parameters to function which will remove all occurrences of given character from string. The entire string except the given character will be displayed as output.

Algorithm

START
Step 1 : Input string variable str and character variable c
Step 2 : Find length of the string
Step 3 : For Loop
			int i = j = 0; i < n; i++
				if str[i] != c 
					str[j++] = str[i];
		Loop End
Step 4 : str[j] = '\0';
STOP


Program To Remove All Occurrences Of A Character In A String

  • C
  • C++
  • Java
  • Python
  • C#
  • #include <stdio.h>
    #include <string.h>
    
    void removeAll(char* str, char c)
    {
     
        int j, n = strlen(str);
        for (int i = j = 0; i < n; i++)
            if (str[i] != c)
                str[j++] = str[i];
     
        str[j] = '\0';
    }
     
    int main()
    {
        char str[] = "letsfindcourse";
        removeAll(str, 'e');
        printf("%s",str);
        return 0;
    }
    
    #include <bits/stdc++.h>
    using namespace std;
     
    void removeAll(char* str, char c)
    {
     
        int j, n = strlen(str);
        for (int i = j = 0; i < n; i++)
            if (str[i] != c)
                str[j++] = str[i];
     
        str[j] = '\0';
    }
     
    int main()
    {
        char str[] = "letsfindcourse";
        removeAll(str, 'e');
        cout << str;
        return 0;
    }
    
    
    public class LFC
    {
    	static void removeAll(String str, char c)
    {
        int j, count = 0, n = str.length();
        char []t = str.toCharArray();
        for (int i = j = 0; i < n; i++)
        {
            if (t[i] != c)
            t[j++] = t[i];
            else
                count++;
        }
         
        while(count > 0)
        {
            t[j++] = '\0';
            count--;
        }
         
        System.out.println(t);
    }
     
    
    public static void main(String[] args)
    {
        String str = "letsfindcourse";
        removeAll(str, 'e');
    }
    }
    
    
    def removeChar(str1, c) :
         
        counts = str1.count(c)
     
        str1 = list(str1)
     
        while counts :
             
            str1.remove(c)
     
            counts -= 1
     
        str1 = '' . join(str1)
         
        print(str1)
     
    
    if __name__ == '__main__' :
         
        str1 = "letsfindcourse"
        removeChar(str1,'e')
         
    
    using System;
     
    class LFC
    {
    static void removeAll(string str,
                           char c)
    {
        int j, count = 0, n = str.Length;
        char[] t = str.ToCharArray();
        for (int i = j = 0; i < n; i++)
        {
            if (str[i] != c)
            t[j++] = str[i];
            else
                count++;
        }
         
        while(count > 0)
        {
            t[j++] = '\0';
            count--;
        }
         
        Console.Write(t);
    }
     
    // Driver Code
    public static void Main()
    {
        string str = "letsfindcourse";
        removeAll(str, 'e');
    }
    }
    
    #include <stdio.h>
    #include <string.h>
    
    void removeAll(char* str, char c)
    {
     
        int j, n = strlen(str);
        for (int i = j = 0; i < n; i++)
            if (str[i] != c)
                str[j++] = str[i];
     
        str[j] = '\0';
    }  
     
    int main()
    {
        char str[] = "letsfindcourse";
        removeAll(str, 'e');
        printf("%s",str);
        return 0;
    }
    

    Output

    ltsfindcours.
    


    Recommended Programs

       Program to reverse a string.
       Program to print fibonacci series.