Deletion of character in String

To delete any Character in a String, we need:

  • A character that is to deleted in the string (say “ch”)

Below is the implementation of the above approach:

C++
// C++ program to remove a particular character
// from a string.
#include <bits/stdc++.h>
using namespace std;

void removeChar(char* s, char c)
{

    int j, n = strlen(s);
    for (int i = j = 0; i < n; i++)
        if (s[i] != c)
            s[j++] = s[i];

    s[j] = '\0';
}

int main()
{
    char s[] = "w3wiki";
    removeChar(s, 'g');
    cout << s;
    return 0;
}
C
#include <stdio.h>
#include <string.h>

void removeChar(char* s, char c) {
    int i, j, n = strlen(s);
    for (i = j = 0; i < n; i++) {
        if (s[i] != c) {
            s[j++] = s[i];
        }
    }
    s[j] = '\0';
}

int main() {
    char s[] = "w3wiki";
    removeChar(s, 'g');
    printf("%s", s);
    return 0;
}
Java
public class Main {
       // Function to remove a particular character 
      // from a character array
     // Parameters:
    // - s: the character array from which 
    // the character will be removed
    // - c: the character to be removed
    public static void removeChar(char[] s, char c) {
        // Initialize a pointer j to keep track of the 
      // position where characters are being moved
        int j = 0;
        // Loop through the character array
        for (int i = 0; i < s.length; i++) {
            // If the current character is 
           // not the one to be removed
            if (s[i] != c) {
                // Move the character to the 
               // position indicated by j
                s[j++] = s[i];
            }
        }
        // Fill the remaining positions with null characters ('\0')
        while (j < s.length) {
            s[j++] = '\0';
        }
    }

    public static void main(String[] args) {
        // Input string as a character array
        char[] s = "w3wiki".toCharArray();
        // Remove character 'g' from the string
        removeChar(s, 'g');
        // Print the modified string
        System.out.println(s);
    }
}
//This code is added By prachi
Python
# Python program to remove a particular character
# from a string.

def removeChar(s, c):
    # Initialize variables
    n = len(s)
    j = 0
    
    # Iterate through each character of the string
    for i in range(n):
        # If the character is not equal to 
        # the target character, keep it
        if s[i] != c:
            s[j] = s[i]
            j += 1
    
    # Add null character at the end of the modified string
    s = s[:j]
    
    return s

if __name__ == "__main__":
    s = "w3wiki"
# Convert string to list of characters for modification
    s = list(s)  
    s = removeChar(s, 'g')
# Convert list of characters back to string for printing
    print(''.join(s))  
#this code is contributed by Adarsh .
JavaScript
// JavaScript program for the above approach

// Function to remove a particular character from a character array
// Parameters:
// - s: the character array from which the character will be removed
// - c: the character to be removed
function removeChar(s, c) {
    // Initialize a pointer j to keep track of the position where characters are being moved
    let j = 0;
    // Loop through the character array
    for (let i = 0; i < s.length; i++) {
        // If the current character is not the one to be removed
        if (s[i] !== c) {
            // Move the character to the position indicated by j
            s[j++] = s[i];
        }
    }
    // Fill the remaining positions with null characters ('\0')
    while (j < s.length) {
        s[j++] = '\0';
    }
}

// Input string as a character array
let s = "w3wiki".split('');
// Remove character 'g' from the string
removeChar(s, 'g');
// Print the modified string
console.log(s.join(''));

// This code is contributed by Susobhan Akhuli

Output
eeksforeeks

Basic String Operations with Implementation

In this post, we will look into some of the basic String operations such as:

  • Accessing characters by index in a string.
  • Inserting character into a String.
  • Modifying character in String
  • Deletion of Character in String
  • Concatenating strings (combining multiple strings into one).
  • Finding the length of a string
  • Comparing strings for equality or lexicographical order

Let us consider the basic String operations one by one.

Similar Reads

Accessing characters by index in a string.

To access any character in a String, we need:...

Inserting Character/String into an String.

To insert any Character/String in a String, we need:...

Modifying character in String

To modify any Character in a String, we need:...

Deletion of character in String

To delete any Character in a String, we need:...

Concatenating strings (combining multiple strings into one).

To concatenate any String to a String, we need:...

Finding the length/size of a string

To find the length of the String, we need:...

Comparing Strings for Equality

To compare strings, Define a function to compare values with the following conditions :...

Contact Us