Open In App

Remove a Character from a Given Position

Last Updated : 04 Mar, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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 = ""

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.

C++
#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;
}
C
#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
// 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(s.deleteCharAt(1));
	}
}
Python
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))
C#
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));
    }
}
JavaScript
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
bbc

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.

C++
#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;
}
C
#include <stdio.h>
#include <string.h>

void remove_char_at_position(char *s, int p) {
    // Check for valid position
    if (p < 0 || p >= strlen(s)) {
        return;
    }

    // Shift characters to the left from the position
    for (int i = p; 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 p = 1;
    remove_char_at_position(s, p);
    printf("%s\n", s);
    return 0;
}
Java
import java.util.*;
public class GFG {

    public static String removechar(String word, char ch)
    {
        StringBuilder s = new StringBuilder(word);
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == ch) {
                s.deleteCharAt(i);
                i--;
            }
        }

        return s.toString();
    }

    // driver's code
    public static void main(String args[])
    {
        String word = "geeksforgeeks";
        char ch = 'e';
        System.out.println(removechar(word, ch));
    }
}
Python
# Remove character at specified position using a loop

def remove_char_at_position(s, p):
  
    # Check for valid position
    if p < 0 or p >= 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(p, len(s) - 1):
        s_list[i] = s_list[i + 1]

    # Remove the last character
    s_list.pop()  
    
    return ''.join(s_list) 
  
s = "abcde" 
p = 1 
s = remove_char_at_position(s, p)  
print(s)  
C#
using System;

class GfG {
    static void Main() {
        string s = "abcde";
        int p = 1;
        s = RemoveCharAtPosition(s, p);
        Console.WriteLine(s);
    }

    static string RemoveCharAtPosition(string s, int p) {
        // Check for valid position
        if (p < 0 || p >= 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 = p; 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);
    }
}
JavaScript
// Remove character at specified position using a loop
function removeCharAtPosition(s, p) {

    // Check for valid position
    if (p < 0 || p >= 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 = p; 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 p = 1;  
s = removeCharAtPosition(s, p);  
console.log(s); 

Output
eeksforeeks

Time Complexity: O(n)
Auxiliary Space: O(1)



Next Article
Article Tags :
Practice Tags :

Similar Reads