Deletion of character in String using Built-in Functions

We will use the built-in functions or methods provided by the respective programming languages to delete the character at the specified position in the string.

Below is the implementation:

C++
#include <iostream>
#include <cstring>

using namespace std;

int main() {
    string str = "w3wiki";
    int pos = 5;
    
    str.erase(pos, 1);
    
    cout << "Modified string: " << str << endl;
    
    return 0;
}
C
#include <stdio.h>
#include <string.h>

int main() {
    char str[50] = "w3wiki";
    int pos = 5;
    
    memmove(str + pos, str + pos + 1, strlen(str) - pos);
    
    printf("Modified string: %s\n", str);
    
    return 0;
}
Java
public class Main {
    public static void main(String[] args) {
        StringBuilder str = new StringBuilder("w3wiki");
        int pos = 5;
        
        str.deleteCharAt(pos);
        
        System.out.println("Modified string: " + str);
    }
}
Python3
str = "w3wiki"
pos = 5

modified_str = str[:pos] + str[pos+1:]

print("Modified string:", modified_str)
JavaScript
let str = "w3wiki";
let pos = 5;

let modified_str = str.substring(0, pos) + str.substring(pos + 1);

console.log("Modified string:", modified_str);

Output
Modified string: GeeksorGeeks

Time Complexity: O(n) where n is the length of the string.
Auxiliary Space: O(n) where n is the length of the string.



Deletion of character in String

Given a string str and an integer position pos, the task is to delete the character at the specified position pos from the string str.

Examples:

Input: str = “w3wiki”, pos = 5
Output: GeeksorGeeks

Input: str = “HelloWorld”, pos = 0
Output: elloWorld

Similar Reads

Deletion of character in String using Loop:

Traverse the string and push all the characters to another string or character array except the character which needs to be deleted. We will shift the characters to the left starting from the specified position to delete the character....

Deletion of character in String using Built-in Functions:

We will use the built-in functions or methods provided by the respective programming languages to delete the character at the specified position in the string....

Contact Us