Deletion of character in String (original) (raw)

Last Updated : 23 Apr, 2024

Try it on GfG Practice redirect icon

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:

C++ `

#include #include

using namespace std;

string delete_char_cpp(string s, char ch) { string new_str = ""; for (char c : s) { if (c != ch) { new_str += c; } } return new_str; }

int main() { string s = "hello"; char ch = 'l'; cout << delete_char_cpp(s, ch) << endl; // Output: "heo" return 0; }

C

#include <stdio.h> #include <string.h>

char* deleteCharC(char* s, char ch) { int i, j; int len = strlen(s); for (i = j = 0; i < len; i++) { if (s[i] != ch) { s[j++] = s[i]; } } s[j] = '\0'; return s; }

int main() { char s[] = "hello"; char ch = 'l'; printf("%s\n", deleteCharC(s, ch)); // Output: "heo" return 0; }

Java

class DeleteCharJava { public static String deleteCharJava(String s, char ch) { StringBuilder newStr = new StringBuilder(); for (char c : s.toCharArray()) { if (c != ch) { newStr.append(c); } } return newStr.toString(); }

public static void main(String[] args) {
    String s = "hello";
    char ch = 'l';
    System.out.println(deleteCharJava(s, ch));  // Output: "heo"
}

}

Python3

def delete_char_python(s, char): new_str = "" for c in s: if c != char: new_str += c return new_str

Example Usage

s = "hello" char = "l" print(delete_char_python(s, char)) # Output: "heo"

JavaScript

function deleteCharJS(s, char) { let newStr = ""; for (let c of s) { if (c !== char) { newStr += c; } } return newStr; }

// Example Usage let s = "hello"; let char = "l"; console.log(deleteCharJS(s, char)); // Output: "heo"

`

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:

C++ `

#include #include

using namespace std;

int main() { string str = "GeeksforGeeks"; int pos = 5;

str.erase(pos, 1);

cout << "Modified string: " << str << endl;

return 0;

}

C

#include <stdio.h> #include <string.h>

int main() { char str[50] = "GeeksforGeeks"; int pos = 5;

memmove(str + pos, str + pos + 1, strlen(str) - pos);

printf("Modified string: %s\n", str);

return 0;

}

Java

public class Main { public static void main(String[] args) { StringBuilder str = new StringBuilder("GeeksforGeeks"); int pos = 5;

    str.deleteCharAt(pos);
    
    System.out.println("Modified string: " + str);
}

}

Python3

str = "GeeksforGeeks" pos = 5

modified_str = str[:pos] + str[pos+1:]

print("Modified string:", modified_str)

JavaScript

let str = "GeeksforGeeks"; 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.