Question

C++ Language:

Write a function, called sort Lbefore e that can sort words into whether or not they conform to the 1 beforee, except after

TestCase:

INPUT OF THE TEST CASE #include < vector> 2 #include <string> 1 4 using Words - std::vector< std::string>; using Word std::st

Write a function, called "sort Lbefore e that can sort words into whether or not they conform to the 1 beforee, except after c rule, See htrps//en.oxtorddictionaries.com/spelling/-before-e-except-after-cl. This function takes 4 references to vectors of strings as parameters . The first vector is the input consisting of lowercase words (only alphabetic charactersl. The next vector should be filled with words that follow the rule The next vector should be filled with words that don't have ie or 'ei in them. The function returns void. You can assume that no word has multiple e's/"ei's in it for simplicity.
INPUT OF THE TEST CASE #include 2 #include 1 4 using Words - std::vector; using Word std::string; - 6 7 Words input = {"happy", "achieve", "ceiling", "beige", "species", "eight", "ei", "ie", "cie", "cei", "thei", "thie", "thcie", "thcei", "ten' 10 }; Words follows_rule, violates_rule, inapplicable_rule; 11 12 13 { "achieve", "ceiling", "ie", "cei", "thie", "thcei" }; { "beige", "species", "eight", "ei", "cie", "thei", "thcie" }; { "happy", "ten" }; 14 Words fol lows_rule_expected Words violates_rule_expected 16 Words inapplicable_rule_expected 15 =
0 0
Add a comment Improve this question Transcribed image text
Answer #1

CODE Explanation in code Comments :

void sort_i_before_e(Words &input , Words &follows_rule,Words &violates_rule,Words &inapplicable_rule){
   for(int i=0;i<input.size();i++){
       //Find "ei","ie" in the vector
       size_t ie = input[i].find("ie");
       size_t ei = input[i].find("ei");
       if(ie!=string::npos){
           //Rule : if "cie" exits in input[i] then goes violates_rule
           //Rule : if "cie" not exits in input[i] then goes follows_rule
       if(input[i][ie-1]!='c'){
       follows_rule.push_back(input[i]);
       }else{
       violates_rule.push_back(input[i]);
       }
       }else if(ei != string::npos){
           //Rule : if "cei" exits in input[i] then goes follows_rule
           //Rule : if "ei" exits in input[i] then goes violates_rule
       if(input[i][ei-1]=='c'){
       follows_rule.push_back(input[i]);
       }else{
       violates_rule.push_back(input[i]);
       }
       }else{
           //"ei","ie" not exists then goes to inapplicable_rule
       inapplicable_rule.push_back(input[i]);
       }
   }
}

Entire Code :

#include<iostream>
#include<vector>
#include<string>
using namespace std;

using Words = vector<string>;
using Word = string;
void sort_i_before_e(Words &input , Words &follows_rule,Words &violates_rule,Words &inapplicable_rule){
   for(int i=0;i<input.size();i++){
       //Find "ei","ie" in the vector
       size_t ie = input[i].find("ie");
       size_t ei = input[i].find("ei");
       if(ie!=string::npos){
           //Rule : if "cie" exits in input[i] then goes violates_rule
           //Rule : if "cie" not exits in input[i] then goes follows_rule
       if(input[i][ie-1]!='c'){
       follows_rule.push_back(input[i]);
       }else{
       violates_rule.push_back(input[i]);
       }
       }else if(ei != string::npos){
           //Rule : if "cei" exits in input[i] then goes follows_rule
           //Rule : if "ei" exits in input[i] then goes violates_rule
       if(input[i][ei-1]=='c'){
       follows_rule.push_back(input[i]);
       }else{
       violates_rule.push_back(input[i]);
       }
       }else{
           //"ei","ie" not exists then goes to inapplicable_rule
       inapplicable_rule.push_back(input[i]);
       }
   }
       //print vector
   for(int i=0;i<follows_rule.size();i++){
       cout<<follows_rule[i]<<" ";
   }
   cout<<endl;
   for(int i=0;i<violates_rule.size();i++){
       cout<<violates_rule[i]<<" ";
   }
   cout<<endl;
   for(int i=0;i<inapplicable_rule.size();i++){
       cout<<inapplicable_rule[i]<<" ";
   }
   cout<<endl;
}
int main(){
   //Initializing vectors
   Words input = {"happy","achieve","ceiling","beige","species","eight","ei","ie","cie","cei","thei","thie","thcie","thcei","ten"};
   Words follows_rule,violates_rule,inapplicable_rule;
   sort_i_before_e(input,follows_rule,violates_rule,inapplicable_rule);
}

Untitled1 - Dev-C++ 5.11 Eile Edit Search View Project Eecute Iools AStyle Window Help TOM-CCc 4.s.2 4-bit Release (globals eOutput Follows_rule achieve ceiling ie cei thie thcei Violates_rule : beige species eight ei cie thei thcie Inapplicable_rule

Add a comment
Know the answer?
Add Answer to:
C++ Language: TestCase: Write a function, called "sort Lbefore e that can sort words into whether or not they conf...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++ Language, Only the sort_i_before_e func, not included main. Also, plz look at the instructions. use...

    C++ Language, Only the sort_i_before_e func, not included main. Also, plz look at the instructions. use vector. Write a function, called "sort Lbefore e that can sort words into whether or not they conform to the 1 beforee, except after c rule, See htrps//en.oxtorddictionaries.com/spelling/-before-e-except-after-cl. This function takes 4 references to vectors of strings as parameters . The first vector is the input consisting of lowercase words (only alphabetic charactersl. The next vector should be filled with words that follow the...

  • C++ Language: Do not use namespace Test Case: 3-D vectors Write a templated function, called "add 3d", that can...

    C++ Language: Do not use namespace Test Case: 3-D vectors Write a templated function, called "add 3d", that can add two const references to 3-d vectors [vector<vector-vector<T>>>]. It should return a new 3-d vector that performed element-wise addition. You cannot use indexing on this problem, instead you must use iterators to access the values of end vector. INPUT OF THE TEST CASE 1 #include <vector> 2 using std: : vector; 3 const vector<double> a{1, 4, -6}; const vector<double> b{3, 6,...

  • Write a French/English dictionary lookup program. Read a list of pairs of English and French words...

    Write a French/English dictionary lookup program. Read a list of pairs of English and French words from a file specified by the user. English/French words should be exact matches (don't try to find partial matches). Use the supplied EnglishFrenchDictionary.java class as your main class. Fill in the missing code in the DictionaryTable.java class to read the input file and perform the searches. Add code to the DictionaryTable read() method to: read pairs of lines (English word is on the first...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT