Write a program in C++ that accepts a string from the user as input and checks t
ID: 668417 • Letter: W
Question
Write a program in C++ that accepts a string from the user as input and checks to see if it is an allowable string of Eng++. The output is simply the message: Your input is grammatical Eng++ or Your input is not grammatical Eng++. There are a finite number of grammatical Eng++ sentences - “around 50”. You are not allowed to use “around 50” C++ if statements to identify grammatical or ungrammatical sentences of Eng++.
Definition of Eng++:
• three suffixes or “markers”:
-ga (marks a noun as the subject of a sentence)
-o (marks a noun as the direct object of the verb)
-ka (marks the verb in a sentence only if the sentence is a question)
Note that adjectives are not marked.
• three verbs: caught, kissed, is
• three nouns: man, woman, fish
• three adjectives: strong, short, red •
Sentences may not begin with a space, but words are separated by exactly one space.
• Word order:
Both declarative sentences and questions have exactly the same word order. The subject is the first word in the sentence, the verb is the last. If the verb is either “caught” or “kissed”, then the direct object (a noun) is in the middle, otherwise an adjective is in the middle. o Declarative sentences do not end with a period in Eng++, but questions do end with the marker –ka on the last word of the question which is a verb.
examples:
woman-ga man-o kissed
fish-ga strong is
man-ga fish-o caught
man-ga red is-ka
fish-ga short is
Explanation / Answer
#include <bits/stdc++.h>
using namespace std;
bool en(string s){
if (s[0] == 'w' || s[0] == 'W' || s[0] == 'm' || s[0] == 'M' || s[0] == 'f' || s[0] == 'F'){
size_t found = s.find("-ga");
if (found != string::npos){
int len = s.length();
if (s[len-1] == 'd' || s[len-1] == 'D' || s[len-1] == 't' || s[len-1] == 'T' || s[len-1] == 's' || s[len-1] == 'S') return true;
else{
size_t found = s.find("-ka")
if (found != string::npos)
return true;
return false;
}
}
return false;
}
return false;
}
int main(){
string s;
cout << "Enter the String : ";
cin >> s;
cout << en(s) << endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.