Program for removing i-th character from a string
Last Updated :
04 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.
Program for removing i-th character from a string using inbuilt function:
Below table represents inbuilt functions used to remove the i-th character from a string in different languages:
Language | Inbuilt Function(s) |
---|
C++ | std::string::erase |
Java | StringBuilder.deleteCharAt |
Python | String slicing (str[:i] + str[i+1:] ) |
C# | string.Remove |
JavaScript | String slicing (str.slice(0, i) + str.slice(i+1) ) |
Below is the implementation:
C++
//C++ code to show use of erase() function
#include <bits/stdc++.h>
using namespace std;
//Method to use erase() function
string removeCharAtIndex(string inputString, int index)
{
//Checking for valid index and removing that portion
if (index >= 0 && index < inputString.length()) {
inputString.erase(index, 1);
}
return inputString;
}
//Main Function
int main()
{
//Input
string inputString = "Hello, World!";
int index = 7;
//Function call
string result = removeCharAtIndex(inputString, index);
cout << result;
return 0;
}
Java
//Java code to show use of deleteCharAt
public class Main {
// Method to use deleteCharAt() function
public static String removeCharAtIndex(String inputString, int index) {
// Checking for valid index and removing that character
if (index >= 0 && index < inputString.length()) {
StringBuilder sb = new StringBuilder(inputString);
sb.deleteCharAt(index);
return sb.toString();
}
return inputString;
}
// Main Function
public static void main(String[] args) {
// Input
String inputString = "Hello, World!";
int index = 7;
// Function call
String result = removeCharAtIndex(inputString, index);
System.out.println(result);
}
}
Python3
# Method to use erase() function
def remove_char_at_index(input_string, index):
# Checking for valid index and removing that portion
if 0 <= index < len(input_string):
input_string = input_string[:index] + input_string[index+1:]
return input_string
# Main Function
def main():
# Input
input_string = "Hello, World!"
index = 7
# Function call
result = remove_char_at_index(input_string, index)
print(result)
if __name__ == "__main__":
main()
C#
using System;
class Program
{
// Method to use the RemoveAt() function
static string RemoveCharAtIndex(string inputString, int index)
{
// Checking for a valid index and removing that portion
if (index >= 0 && index < inputString.Length)
{
inputString = inputString.Remove(index, 1);
}
return inputString;
}
// Main Function
static void Main()
{
// Input
string inputString = "Hello, World!";
int index = 7;
// Function call
string result = RemoveCharAtIndex(inputString, index);
Console.WriteLine(result);
}
}
// This code is contributed by shivamgupta0987654321
JavaScript
// JavaScript code to show use of slice() function
// Method to remove a character at a specific index
function removeCharAtIndex(inputString, index) {
// Checking for a valid index and removing that character
if (index >= 0 && index < inputString.length) {
inputString = inputString.slice(0, index) + inputString.slice(index + 1);
}
return inputString;
}
// Input
let inputString = "Hello, World!";
let index = 7;
// Function call
let result = removeCharAtIndex(inputString, index);
console.log(result);
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:
C++
#include <bits/stdc++.h>
using namespace std;
string removeCharAtIndex(string& inputString, int index)
{
if (index >= 0 && index < inputString.length()) {
string result;
// Copy characters from the original string to the
// result string, excluding the character at the
// specified index
for (int i = 0; i < inputString.length(); i++) {
if (i != index) {
result.push_back(inputString[i]);
}
}
return result;
}
else {
return inputString;
}
}
int main()
{
// Input
string inputString = "Hello";
int index = 1;
// Function_call
string result = removeCharAtIndex(inputString, index);
cout << result;
return 0;
}
Java
// Java Implementation
public class RemoveCharAtIndex {
public static String removeCharAtIndex(String inputString, int index) {
if (index >= 0 && index < inputString.length()) {
StringBuilder result = new StringBuilder();
// Copy characters from the original string to the
// result string, excluding the character at the
// specified index
for (int i = 0; i < inputString.length(); i++) {
if (i != index) {
result.append(inputString.charAt(i));
}
}
return result.toString();
} else {
return inputString;
}
}
public static void main(String[] args) {
// Input
String inputString = "Hello";
int index = 1;
// Function call
String result = removeCharAtIndex(inputString, index);
System.out.println(result);
}
}
// This code is contributed by Sakshi
Python
def removeCharAtIndex(inputString, index):
if 0 <= index < len(inputString):
result = ''
# Copy characters from the original string to the
# result string, excluding the character at the
# specified index
for i in range(len(inputString)):
if i != index:
result += inputString[i]
return result
else:
return inputString
# Input
inputString = "Hello"
index = 1
# Function call
result = removeCharAtIndex(inputString, index)
print(result)
JavaScript
// JavaScript Implementation
function removeCharAtIndex(inputString, index) {
if (index >= 0 && index < inputString.length) {
let result = "";
// Copy characters from the original string to the
// result string, excluding the character at the
// specified index
for (let i = 0; i < inputString.length; i++) {
if (i !== index) {
result += inputString.charAt(i);
}
}
return result;
} else {
return inputString;
}
}
// Input
const inputString = "Hello";
const index = 1;
// Function call
const result = removeCharAtIndex(inputString, index);
console.log(result);
Time Complexity: O(N)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...