Deleting a character from a string. Write a program which asks the user to enter
ID: 3676388 • Letter: D
Question
Deleting a character from a string. Write a program which asks the user to enter a C-string str and an integer i. It is supposed to modify the char-array str by deleting its i'th character, where i = 0 corresponds to the first character, i = 1 corresponds to the second character, and so on ....At the end your program is supposed to print the modified string. For example, on input Please enter a string:eece230 Please enter i:2 your program is supposed to store "eece230" in str, modify the content of str to "eee230", and finally print eee230 Note that it is not enough to set the deleted character to a blank character; the length of modified string is smaller than that of the original string by one.Explanation / Answer
Solution.cpp
#include <iostream>//input output fuctions
#include <string>//string functions
using namespace std;//using namespace std; tells the compiler to use the std namespace.
int main ()
{//programstarts frm main
string str;
int i;
cout << "Please Enter the string:" << ' ';
cin >> str;
cout<<"Pease enter the i: ";
cin>>i;
// "This is an sentence."
str.erase (str.begin()+i); //this erase function erases the ith character from the string // ^
cout << str << ' ';
// "This is a sentence.
// "This sentence."
return 0;
}
output
Please Enter the string: eece230 Pease enter the i: 2 eee230
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.