In this question you have to write a program consisting of two functions. A string has to be input in the main
function and then a string function, namely shorterString, has to be called to shorten the string by deleting
(“erasing”) every third character of the string. This has to be done repeatedly until the length of the string becomes
less than 6 and then the final string should be displayed by the main function. No global variables may be used.
Suppose, for example, the string
Hello world!
is input. Then
Helowold
should be returned to the main function by shorterString. Then, when the function is called again, the string
Heowld
should be returned. Then, when the function is called again, the string
Hewl
should be returned. This will be the final result (because its length is less than 6) and should be displayed.
Repeat the whole process described above for a number of strings until the string END is input. Use loops as
required. Run your program on the strings below and submit printouts of the program and output.
Cheers!
String manipulation is really not difficult.
Only two questions remain to be done.
Below is my code - but cannot get it to remove every 3rd character in a string - Help Please
using namespace std;
void shorterstring(string text)
{
for(int i =0; i <=0; i++)
{text.erase(3,1);
}
cout<<text<<endl;
}
int main()
{
string sentence;
cout << " Enter sentence: ";
getline(cin,sentence,'\n');
shorterstring(sentence);
return 0;
}