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: GeeksorGeeksInput: str = "HelloWorld", pos = 0
Output: elloWorld
Using Loop - O(n) Time and O(n) Space
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:
- Initialize a character array to store the modified string.
- Traverse the original string character by character.
- If the current index matches the specified position, skip the character.
- Otherwise, copy the current character from the original string to the modified string.
- Null-terminate the modified string.
#include <iostream>
#include <string>
using namespace std;
string deleteChar(string s, int pos) {
string newStr = "";
// Build a new string by skipping the
// character at the given position.
for (int i = 0; i < s.length(); i++) {
if (i != pos) {
newStr += s[i];
}
}
return newStr;
}
int main() {
string s = "GeeksforGeeks";
int pos = 5;
cout << deleteChar(s, pos) << endl;
return 0;
}
#include <stdio.h>
#include <string.h>
char* deleteChar(char* s, int pos) {
int i, j;
int len = strlen(s);
// Build a new string by skipping the
// character at the given position.
for (i = j = 0; i < len; i++) {
if (i != pos) {
s[j++] = s[i];
}
}
s[j] = '\0';
return s;
}
int main() {
char s[] = "GeeksforGeeks";
int pos = 5;
printf("%s\n", deleteChar(s, pos));
return 0;
}
class GFG {
public static String deleteChar(String s, int pos) {
StringBuilder newStr = new StringBuilder();
// Build a new string by skipping the
// character at the given position.
for (int i = 0; i < s.length(); i++) {
if (i != pos) {
newStr.append(s.charAt(i));
}
}
return newStr.toString();
}
public static void main(String[] args) {
String s = "GeeksforGeeks";
int pos = 5;
System.out.println(deleteChar(s, pos));
}
}
def deleteChar(s, pos):
newStr = ""
# Build a new string by skipping the character at the given position.
for i in range(len(s)):
if i != pos:
newStr += s[i]
return newStr
if __name__ == "__main__":
s = "GeeksforGeeks"
pos = 5
print(deleteChar(s, pos))
using System;
class GFG {
public static string deleteChar(string s, int pos) {
string newStr = "";
// Build a new string by skipping the character at the given position.
for (int i = 0; i < s.Length; i++) {
if (i != pos) {
newStr += s[i];
}
}
return newStr;
}
public static void Main() {
string s = "GeeksforGeeks";
int pos = 5;
Console.WriteLine(deleteChar(s, pos));
}
}
function deleteChar(s, pos) {
let newStr = "";
// Build a new string by skipping the
// character at the given position.
for (let i = 0; i < s.length; i++) {
if (i !== pos) {
newStr += s[i];
}
}
return newStr;
}
// Driver code
let s = "GeeksforGeeks";
let pos = 5;
console.log(deleteChar(s, pos));
Output
GeeksorGeeks
Using Built-in Functions - O(n) Time and O(1) Space
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.
#include <iostream>
#include <cstring>
using namespace std;
int main() {
string str = "GeeksforGeeks";
int pos = 5;
str.erase(pos, 1);
cout << str << endl;
return 0;
}
#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("%s", str);
return 0;
}
public class Main {
public static void main(String[] args) {
StringBuilder str = new StringBuilder("GeeksforGeeks");
int pos = 5;
str.deleteCharAt(pos);
System.out.println(str);
}
}
str = "GeeksforGeeks"
pos = 5
modified_str = str[:pos] + str[pos+1:]
print(modified_str)
using System;
class GFG {
public static void Main() {
string str = "GeeksforGeeks";
int pos = 5;
string modifiedStr = str.Remove(pos, 1);
Console.WriteLine(modifiedStr);
}
}
let str = "GeeksforGeeks";
let pos = 5;
let modified_str = str.substring(0, pos) + str.substring(pos + 1);
console.log("Modified string:", modified_str);
Output
GeeksorGeeks