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)
Similar Reads
How to remove the first character of string in PHP? Remove the very first character of a given string in PHP Examples: Input : GeeksforgeeksOutput : eeksforgeeksInput :, Hello geek!Output : Hello geek!Explanation:In PHP to remove characters from the beginning we can use ltrim but in that, we have to define what we want to remove from a string i.e. re
3 min read
Remove all characters other than alphabets from string Given a string consisting of alphabets and others characters, remove all the characters other than alphabets and print the string so formed. Examples: Input : $Gee*k;s..fo, r'Ge^eks?Output : GeeksforGeeks Input : P&ra+$BHa;;t*ku, ma$r@@s#in}ghOutput : PraBHatkumarsingh Recommended PracticeRemove
12 min read
Remove Last character from String in Linux In this article, we will discuss how to remove the last character from the string in Linux. In Linux, there are various commands and techniques present by which you can do this task in an easier way if you have some basic knowledge about the commands in Linux. Here, you will see the different comman
3 min read
How to Remove Last Character from String in Ruby? Removing the last character from a string in Ruby is a common task in various programming scenarios. Whether you need to manipulate user input, process file paths, or clean up data, there are several approaches to achieve this. This article focuses on discussing how to remove the last character from
2 min read
Remove characters from the first string which are present in the second string Given two strings string1 and string2, remove those characters from the first string(string1) which are present in the second string(string2). Both strings are different and contain only lowercase characters.NOTE: The size of the first string is always greater than the size of the second string( |st
15+ min read