Program is not running gives errors. here are the instruction. Write a C++ progr
ID: 670768 • Letter: P
Question
Program is not running gives errors.
here are the instruction.
Write a C++ program named hw03.cpp that queries the user for an input string and a 3-character substring. Then perform the 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 the substring is not
found
Print location of the substring. Position 0 is the first character of the string.
Remove the substring from the input string.
Determine if the new string is a “magic” string, in which case print “Magic string!”.
A “magic” string is one for which the length is a multiple of 5, and the 5th character is a vowel. You program must have the following:
Three lines of comments at the top with your name, program name, and the date created.
Use variables of type string
Use getline() function to obtain a phrase that may contain spaces
Using constant string::npos to determine when the substring does not appear
Use string function replace() to remove the substring
Use string function find_first_of() to find if vowel at the magic position
Use compound logical statement when determining whether a magic string
Sample Output (four possibilities shown)
heres what Ihave so far.
#include
#include
using namespace std;
int main( )
{
// Defining variables.
//Initilizing variables.
int magicNum;
int magicNum1;
int position;
long int phraselength;
string vowels = "aeiou";
string phrase;
string sub;
// The phrase and the sub phrase
cout << "Enter a phrase:" << endl;
std::getline (std::cin,phrase);
cout << "Enter a 3-character sub:" << endl;
cin >> sub;
if (sub.length()!=3)
{
cout << "Incorrect length of substring" << endl;
}
else if(phrase.find(sub)!=std::string::npos){
cout << "Substring occurs " << phrase.find(sub) << endl;
long position = phrase.find(sub);
// removes the substring.
cout << "Phrase without substring is:" << phrase.replace(position,3,"") << endl;
//finding length of magic string
phraselength = phrase.length();
magicNum = phrase.length() % 5;
long magicNum1 = phrase.find_first_of(vowels);
if (magicNum == 0)
{
if (magicNum1 == 5)
{
cout<<"magic string!"<< endl;
}
}
else
{
cout<<"Not a magic string";
}
}
return 0;
}
Explanation / Answer
#include <iostream>
#include<string>
using namespace std;
int main()
{
string String,sub;
int flag=0;
cout << "Enter the String: " << endl;
getline (std::cin,String);
cout<<"enter the substring of size 3 characters: ";
getline(std::cin,sub);
if(sub.length()==3){
//string str3 = String.substr (sub);
//cout<<"string: "<<str3;
if(String.find(sub) != string::npos){
cout<<"entered string has sub-string"<<endl;
size_t pos = String.find(sub);
cout<<pos<<endl;
String.erase (pos,3);
cout<<"After deleting the string is: "<<String<<endl;
std::size_t found = String.find_first_of("aeiou");
if((String.length()%5)==0){
while (found!=std::string::npos)
{
found=String.find_first_of("aeiou",found+1);
if(found==4){
cout<<"Magic Word"<<endl;
flag=1;
break;
}
}
if(flag==0){
cout<<"Not a magic word!!"<<endl;
}
}
else{
cout<<"not a magic word"<<endl;
}
}
else{
cout<<"substring not found!!!!"<<endl;
}
}else{
cout<<"Error wrong sub string: ";
}
return 0;
}
TRY THIS
#include<iostream>
#include<string>
using namespace std;
int main( )
{
// Defining variables.
//Initilizing variables.
int magicNum;
int magicNum1;
int position;
long int phraselength;
string vowels = "aeiou";
string phrase;
string sub;
// The phrase and the sub phrase
cout << "Enter a phrase:" << endl;
std::getline (std::cin,phrase);
cout << "Enter a 3-character sub:" << endl;
cin >> sub;
if (sub.length()!=3)
{
cout << "Incorrect length of substring" << endl;
}
else if(phrase.find(sub)!=std::string::npos){
cout << "Substring occurs " << phrase.find(sub) << endl;
long position = phrase.find(sub);
// removes the substring.
cout << "Phrase without substring is:" << phrase.replace(position,3,"") << endl;
//finding length of magic string
phraselength = phrase.length();
magicNum = phrase.length() % 5;
long magicNum1 = phrase.find_first_of(vowels);
if (magicNum == 0)
{
if (magicNum1 == 5)
{
cout<<"magic string!"<< endl;
}
}
else
{
cout<<"Not a magic string";
}
}
return 0;
}
your proram is running correctly as the fifth character is a space any try both of this
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.