Program to Delete Given Character From the String
- Write a program to Delete Given Character From the String in C
- Write a program to Delete Given Character From the String in C++
- Write a program to Delete Given Character From the String in Python
- Write a program to Delete Given Character From the String in PHP
- Write a program to Delete Given Character From the String in Java
- Write a program to Delete Given Character From the String in Java Script
- Write a program to Delete Given Character From the String in C#
Explanation:
It is easy to remove a specific character from a string by iterating through it and creating a new string that does not contain the desired character. Here’s the fundamental reasoning:
Logic Explanation:
- Traverse the String:
- Iterate through each character of the string.
- Check for the Character to Remove:
- For each character, check if it matches the character you want to delete.
- Construct the New String:
- If the character matches the one to be deleted, skip it.
- Otherwise, add it to the new string.
- Return the Modified String:
- Once all characters have been checked, the modified string will be the one without the deleted character.
Program to Delete Given Character From the String
-
C
-
C++
-
Python
-
PHP
-
JAVA
-
Java Script
-
C#
#include <iostream>
#include <string>
using namespace std;
void removeCharacter(string &str, char charToRemove) {
str.erase(remove(str.begin(), str.end(), charToRemove), str.end());
}
int main() {
string str;
char charToRemove;
cout << "Enter a string: ";
getline(cin, str);
cout << "Enter the character to remove: ";
cin >> charToRemove;
removeCharacter(str, charToRemove);
cout << "Modified string: " << str << endl;
return 0;
}
def remove_character(string, char_to_remove):
return string.replace(char_to_remove, '')
string = input("Enter a string: ")
char_to_remove = input("Enter the character to remove: ")
result = remove_character(string, char_to_remove)
print("Modified string:", result)
def remove_character(string, char_to_remove):
return string.replace(char_to_remove, '')
string = input("Enter a string: ")
char_to_remove = input("Enter the character to remove: ")
result = remove_character(string, char_to_remove)
print("Modified string:", result)
<?php
function removeCharacter($string, $charToRemove) {
return str_replace($charToRemove, '', $string);
}
echo "Enter a string: ";
$string = trim(fgets(STDIN));
echo "Enter the character to remove: ";
$charToRemove = trim(fgets(STDIN));
$result = removeCharacter($string, $charToRemove);
echo "Modified string: " . $result . "\n";
?>
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a string: ");
String str = scanner.nextLine();
System.out.print("Enter the character to remove: ");
char charToRemove = scanner.next().charAt(0);
String result = str.replace(String.valueOf(charToRemove), "");
System.out.println("Modified string: " + result);
}
}
function removeCharacter(str, charToRemove) {
return str.split(charToRemove).join('');
}
const str = prompt("Enter a string:");
const charToRemove = prompt("Enter the character to remove:");
const result = removeCharacter(str, charToRemove);
console.log("Modified string:", result);
using System;
class Program {
static void Main() {
Console.Write("Enter a string: ");
string str = Console.ReadLine();
Console.Write("Enter the character to remove: ");
char charToRemove = Console.ReadKey().KeyChar;
Console.WriteLine();
string result = str.Replace(charToRemove.ToString(), "");
Console.WriteLine("Modified string: " + result);
}
}