Given a string, an integer position, and an integer length, all on separate lines, replace choiceLen number of characters with "(removed)" starting at that position in the string. Then, output the result.
Ex: If the input is:
Fuzzy bear
3
4
the output is:
Fuz(removed)ear
SOLUTION TO THE ABOVE QUESTION
SOLUTION CODE
#include<iostream>
#include <string>
using namespace std;
int main()
{
string my_string;
cout<<"Enter your string: ";
getline(cin, my_string);
int integer_position;
cout<<"Enter an integer position: ";
cin>>integer_position;
int integer_length;
cout<<"Enter an integer length: ";
cin>>integer_length;
//print your original string
cout<<"\nMy original string is: "<<my_string<<endl;
int my_string_length = my_string.length();
//declare a new string to append toit
string my_new_string = "";
int counter = 0;
while(counter<my_string_length)
{
if(counter==integer_position)
{
string string_to_add = "(removed)";
my_new_string+=string_to_add;
}
else if(counter>integer_position && counter<(integer_length+integer_position))
{
//do nothing
}
else
{
//if character not to be removed
// string(1,x) converts character 'x' to string "x"
my_new_string+=string(1,my_string[counter]); //apend to new str
}
counter = counter + 1;
}
//print your new string
cout<<"\nMy new string is: "<<my_new_string<<endl;
return 0;
}
SAMPLE OUTPUT PROGRAM
Comments
Leave a comment