Given a string and a position (0-based indexing), remove the character at the given position.
Examples:
Input : s = "abcde", pos = 1
Output : s = "acde"
Input : s = "a", pos = 0
Output : s = ""
Try It Yourself
Approach - By Using Built-In Methods
We use erase in C++, string slicing in Python, StringBuilder Delete in Java, substring in Java and slice in JavaScript.
#include <bits/stdc++.h>
using namespace std;
string removeCharAtPosition(string s, int pos) {
if (pos < 0 || pos >= s.length()) {
return s;
}
s.erase(pos, 1);
return s;
}
int main() {
string s = "abcde";
int pos = 1;
cout << "Output: " << removeCharAtPosition(s, pos) << endl;
return 0;
}
#include <stdio.h>
#include <string.h>
void removeCharAtPosition(char *s, int pos) {
int len = strlen(s);
if (pos < 0 || pos >= len) {
return;
}
memmove(&s[pos], &s[pos + 1], len - pos);
}
int main() {
char s[] = "abcde";
int pos = 1;
removeCharAtPosition(s, pos);
printf("Output: %s\n", s);
return 0;
}
// Java program to demonstrate
// the deleteCharAt() Method.
class GFG {
public static void main(String[] args)
{
// create a StringBuilder object
// with a String pass as parameter
StringBuilder s
= new StringBuilder("abcde");
// print string after removal of Character
// at index 1
System.out.println("Output: " + s.deleteCharAt(1));
}
}
def remove_char_at_position(s, pos):
if pos < 0 or pos >= len(s):
return s
return s[:pos] + s[pos+1:]
s = "abcde"
pos = 1
print("Output:", remove_char_at_position(s, pos))
using System;
class GfG
{
static string RemoveCharAtPosition(string s, int pos)
{
if (pos < 0 || pos >= s.Length)
return s;
return s.Substring(0, pos) + s.Substring(pos + 1);
}
static void Main()
{
string s = "abcde";
int pos = 1;
Console.WriteLine("Output: " + RemoveCharAtPosition(s, pos));
}
}
function removeCharAtPosition(s, pos) {
if (pos < 0 || pos >= s.length) {
return s;
}
return s.slice(0, pos) + s.slice(pos + 1);
}
let s = "abcde";
let pos = 1;
console.log("Output: " + removeCharAtPosition(s, pos));
Output
Output: acde
Time Complexity: O(n)
Auxiliary Space: O(1)
Approach - By Writing Your Own Method
We move all characters after the given position, one index back. To do this we mainly do s]i] = s[i+1] for all indexes i after p.
#include <bits/stdc++.h>
using namespace std;
void customRemoveCharAtPosition(string &s, int pos) {
if (pos < 0 || pos >= s.length()) {
return;
}
// Shift characters to the left from the position
for (int i = pos; i < s.length() - 1; i++) {
s[i] = s[i + 1];
}
s.resize(s.length() - 1);
}
int main() {
string s = "abcde";
int pos = 1;
customRemoveCharAtPosition(s, pos);
cout << s << endl;
return 0;
}
#include <stdio.h>
#include <string.h>
void remove_char_at_position(char *s, int pos) {
// Check for valid position
if (pos < 0 || pos >= strlen(s)) {
return;
}
// Shift characters to the left from the position
for (int i = pos; i < strlen(s) - 1; i++) {
s[i] = s[i + 1];
}
// Null terminate the string
s[strlen(s) - 1] = '\0';
}
int main() {
char s[] = "abcde";
int pos = 1;
remove_char_at_position(s, pos);
printf("%s\n", s);
return 0;
}
public class Main {
public static void removeCharAtPosition(StringBuilder s, int pos) {
// Check for valid position
if (pos < 0 || pos >= s.length()) {
return;
}
// Shift characters to the left from the position
for (int i = pos; i < s.length() - 1; i++) {
s.setCharAt(i, s.charAt(i + 1));
}
// Remove the last character
s.deleteCharAt(s.length() - 1);
}
public static void main(String[] args) {
StringBuilder s = new StringBuilder("abcde");
int pos = 1;
removeCharAtPosition(s, pos);
System.out.println(s);
}
}
# Remove character at specified position using a loop
def remove_char_at_position(s, pos):
# Check for valid position
if pos < 0 or pos >= len(s):
return s
# Convert string to list for mutable operations
s_list = list(s)
# Shift characters to the left from the position
for i in range(pos, len(s) - 1):
s_list[i] = s_list[i + 1]
# Remove the last character
s_list.pop()
return ''.join(s_list)
s = "abcde"
pos = 1
s = remove_char_at_position(s, pos)
print(s)
using System;
class GfG {
static void Main() {
string s = "abcde";
int pos = 1;
s = RemoveCharAtPosition(s, pos);
Console.WriteLine(s);
}
static string RemoveCharAtPosition(string s, int pos) {
// Check for valid position
if (pos < 0 || pos >= s.Length) {
return s;
}
// Create a char array for mutable operations
char[] sArray = s.ToCharArray();
// Shift characters to the left from the position
for (int i = pos; i < s.Length - 1; i++) {
sArray[i] = sArray[i + 1];
}
// Create a new string excluding the last character
return new string(sArray, 0, s.Length - 1);
}
}
// Remove character at specified position using a loop
function removeCharAtPosition(s, pos) {
// Check for valid position
if (pos < 0 || pos >= s.length)
return s;
// Convert string to array for mutable operations
let sArr = s.split('');
// Shift characters to the left from the position
for (let i = pos; i < s.length - 1; i++)
sArr[i] = sArr[i + 1];
// Remove the last character
sArr.pop();
return sArr.join(''); // Convert array back to string
}
let s = "abcde";
let pos = 1;
s = removeCharAtPosition(s, pos);
console.log(s);
Output
acde
Time Complexity: O(n)
Auxiliary Space: O(1)