-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring_remove.cpp
More file actions
37 lines (30 loc) · 1.14 KB
/
string_remove.cpp
File metadata and controls
37 lines (30 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <cstring>
/* The below code prmpts a user to enter a string, and a character they want o remove from the string, then replaces the character with a whitespace */
using namespace std;
void delete_char(char *my_string, char my_char) {
// Loop through the String
for (size_t i = 0; i < strlen(my_string); ++i) {
// If the current character matches the character to remove
if (my_string[i] == my_char) {
my_string[i] = ' '; // Replace it with a space
}
}
}
int main() {
// The input string must contain a maximum of 100 characters
const int MAX_LENGTH = 100;
char input_string[MAX_LENGTH];
// Prompt the user for input
cout << "Enter a String: ";
cin.getline(input_string, MAX_LENGTH);
// Prompt the user for the character to delete
char char_to_delete;
cout << "Enter a character to delete: ";
cin >> char_to_delete;
// Call the delete_char function
delete_char(input_string, char_to_delete);
// Output the modified my_string
cout << "Modified my_string: " << input_string << endl;
return 0;
}