Search / verify if a word (string) is infinitive

1

Good morning. My problem is this: I want to implement a program in c ++ that when entering a word, that is, a string, tell me if it is infinitive or not. The program has to be repeated as many times as the user wants (that is, with a while). When the word is infinitive, I mean that the string ends in "ar", "er", "go" (laugh, sing, etc).

I have tried to see if some of these functions on this page could help me: link . But in reality I do not see any usefulness so that I can solve my problem or I do not know how to implement them.

I have doubts in how to do the function that seeks or verifies in a word its endings, that is, if it verifies if it is infinitive or not (if it ends in "-ar", "- er", "- go") . I would greatly appreciate your help and your answers

    
asked by Grecia V. P. Valero 29.03.2017 в 01:38
source

2 answers

0

The function you should use is compare with it you should do something like this

string str1 = "ar";
string str2 = "er";
string str3 = "ir";
string str4;
cin >> str4;
 if (str4.compare(str4.size()-2,2,str1) == 0)
     cout << "termina en ar" <<endl;
 if (str4.compare(str4.size()-2,2,str2) == 0)
     cout << "termina en er" <<endl;
 if (str4.compare(str4.size()-2,2,str3) == 0)
     cout << "termina en ir" <<endl;
    
answered by 29.03.2017 / 01:54
source
1

This problem can be a candidate to be resolved with regular expressions .

One of the possible regular expressions for this purpose would be [A-Za-z]+(?:ar|er|ir) , which applied using the library <regex> (incorporated into C ++ in the 2011 standard) would look like this:

std::regex infinitivo("[A-Za-z]+(?:ar|er|ir)");
std::smatch match;

for (const auto &palabra : palabras)
{
    if (std::regex_match(palabra, match, infinitivo))
    {
        std::cout << '"' << palabra << "\" parece un unfinitivo.\n";
    }
}

The code can be seen working here .

The problem with this approach is that as a verb detector is very bad, any alphabetical text ending in ar , er or ir is candidate to be a verb even if it is not ... so:

  • Kurir would be an infinitive verb candidate even if it is not a Spanish word.
  • Mar would be an infinitive verb candidate even without being a verb.
  • TER would not be a candidate for infinitive verb ... but for being in capital letters not for being a name.

Additionally, to go is an infinitive verb and it would be a false negative! This last problem can be solved by modifying the expression to: (?:[A-Za-z]+(?:ar|er)|[A-Za-z]?ir) but with respect to detecting whether a word is verb or not it would require much more work.

To finish, it is possible to check the regular expression on a text so that it ignores the difference between uppercase and lowercase if the std::regex object is declared in the following way:

std::regex infinitivo("(?:[A-Za-z]+(?:ar|er)|[A-Za-z]?ir)", std::regex_constants::icase);
    
answered by 29.03.2017 в 09:10