VOOZH about

URL: https://www.geeksforgeeks.org/dsa/program-for-removing-i-th-character-from-a-string/

⇱ Program for removing i-th character from a string - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Program for removing i-th character from a string

Last Updated : 4 Apr, 2024

Given a string S along with an integer i. Then your task is to remove ith character from S.

Examples:

Input: S = Hello World!, i = 7
Output: Hello orld!
Explanation: The Xth character is W and after removing it S becomes Hello orld!

Input: S = GFG, i = 1
Output: GG
Explanation: It can be verified that after removing 1st character the string S will be GG.

Below table represents inbuilt functions used to remove the i-th character from a string in different languages:

LanguageInbuilt Function(s)
C++std::string::erase
JavaStringBuilder.deleteCharAt
PythonString slicing (str[:i] + str[i+1:])
C#string.Remove
JavaScriptString slicing (str.slice(0, i) + str.slice(i+1))

Below is the implementation:


Output
Hello, orld!

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

Program for removing i-th character from a string by creating new Temporary String.

We can create a new string and add all the characters of original input string except the ith character.

Below is the implementation of the above approach:


Output
Hllo

Time Complexity: O(N)
Auxiliary Space: O(N)

Comment