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:
#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;
}
#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;
}
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);
}
}
str = "w3wiki"
pos = 5
modified_str = str[:pos] + str[pos+1:]
print("Modified string:", modified_str)
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: GeeksorGeeksInput: str = “HelloWorld”, pos = 0
Output: elloWorld
Contact Us