Please help with homework. Thank you very much. -- query user for input string a
ID: 670605 • Letter: P
Question
Please help with homework. Thank you very much.
-- query user for input string and a 3 character substring...then perform following
check length of substring; print error and return if not equal to a length of 3
find location of the substring in the input string. print error and return if substring not found
print location of substring, position O is the first character of the string.
remove substring from input string
determine if the new string is a "magic string", in which case print "Magic string!"
rules -- magic string-- length is a multiple of 5 and 5th character is a vowel;
use variables of type string; use getline() ; use constant string::npos to determine when string doesn't appear; use string function replace() to remove substring; use string function find_first_of() to find a vowel at the magic position; use compound logical statement when determining whether a magic string
Explanation / Answer
#include<iostream.h>
#include<string>
#include<conio.h>
using namespace std;
int main()
{
string inputstring,substring;
int subindex,sublength;
cout<< "please enter the string";
getLine(cin, inputstring);
cout <<"please enter the substring";
getLine(cin, substring);
sublength = substring.size();
if (sublength==3)
{
cout<<"The substring is valid";
}
else
{
cout<< "error";
cout<< "please enter the calid substring";
getLine(cin, substring);
}
string :: size_type subindex= inputstring.find (substring);
if (subindex != string:: npos)
{
cout<< "substring found at location"<<subindex<<endl;
}
else
{
cout<< "String not found";
}
std::string::size_type i=inputstring.find(substring);
if (i!= std::string::npos)
inputstring.erase (i,substring.length());
cout<<"the string after removal is:"<<inputstring<<endl;
cout<<"the vowel is found in the inputstring";
return 0;
}