VOOZH about

URL: https://www.geeksforgeeks.org/dsa/deletion-of-character-in-string/

⇱ Deletion of character in String - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Deletion of character in String

Last Updated : 23 Apr, 2024

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 = "GeeksforGeeks", pos = 5
Output: GeeksorGeeks

Input: str = "HelloWorld", pos = 0
Output: elloWorld

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.

Step-by-step algorithm:

  1. Initialize a character array to store the modified string.
  2. Traverse the original string character by character.
  3. If the current index matches the specified position, skip the character.
  4. Otherwise, copy the current character from the original string to the modified string.
  5. Null-terminate the modified string.

Below is the implementation of the algorithm:


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 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:


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.

Comment
Article Tags:
Article Tags: