Advertisement
dejadavi

DDMLP6

Apr 27th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.62 KB | None | 0 0
  1. //============================================================================
  2. // Name        : MachineLearningProblem6.cpp
  3. // Author      :
  4. // Version     :
  5. // Copyright   : Your copyright notice
  6. // Description : Hello World in C++, Ansi-style
  7. //============================================================================
  8.  
  9. #include <iostream>
  10. #include <string>
  11. #include <sstream>
  12.  
  13. int vowel_counts(std::string);
  14. int word_counts(std::string);
  15. int char_counts(std::string);
  16. void translatetay(std::string);
  17. void vowel_switch(std::string word);
  18. void consonant_switch(std::string word);
  19.  
  20. int main() {
  21.  
  22.  
  23.     ///Part 1
  24.     std::string phrase;
  25.     int vowels=0, words=0,chars=0;
  26.  
  27.     //Input words
  28.     std::cout<<"Input a word phrase."<<std::endl;
  29.     std::getline(std::cin, phrase);
  30.     //std::cout<<phrase<<std::endl;
  31.  
  32.     //Initial functions for part one for word,vowel and character counters
  33.     vowels=vowel_counts(phrase);
  34.     std::cout<<"There are "<<vowels<<" vowels in the phrase "<<phrase<<std::endl;
  35.  
  36.     words=word_counts(phrase);
  37.     std::cout<<"There are "<<words<<" words in the phrase "<<phrase<<std::endl;
  38.  
  39.     chars=char_counts(phrase);
  40.     std::cout<<"There are "<<chars<<" characters in the phrase "<<phrase<<std::endl;
  41.  
  42.  
  43.     //Part 2
  44.     ///Function to translate and output words
  45.     translatetay(phrase);
  46.  
  47. }
  48.  
  49. int vowel_counts(std::string phrase){
  50.  
  51.     //Function to count the number of vowels in a phrase and return the number as an int
  52.  
  53.     int vowel_count=0;
  54.     std::string vowels="AaEeIiOoUu";
  55.     //Does not include Y in vowels
  56.  
  57.     int first=phrase.find_first_not_of(' ');
  58.     int last=phrase.find_last_not_of(' ');
  59.     phrase=phrase.substr(first, last);
  60.  
  61.     if(phrase.length()<=0){
  62.             //Will quit if only space input or no chars
  63.             vowel_count=0;
  64.         } else {
  65.  
  66.  
  67.     for(std::size_t letter=0, length=phrase.length();letter!=length;++letter){
  68.  
  69.         if(vowels.find(phrase[letter])!=std::string::npos){
  70.  
  71.             vowel_count++;
  72.         } else {;}
  73.  
  74.         }
  75.     }
  76.     return vowel_count;
  77. }
  78.  
  79. int word_counts(std::string phrase){
  80.  
  81.     //Function to count the number of words in a phrase and return the number as an int
  82.  
  83.     //Assumes at least one word, othewise function will be stopped at first if statement
  84.     int word_count=1;
  85.  
  86.     //trim leading all trailing whitespace
  87.     //trim leading an trailing whitespace
  88.     //Assumes at least one word, othewise function will be stopped at first if statement
  89.  
  90.     int first=phrase.find_first_not_of(' ');
  91.     int last=phrase.find_last_not_of(' ');
  92.     phrase=phrase.substr(first, last);
  93.  
  94.     if(phrase.length()<=0){
  95.         //Will quit if only space input or no chars
  96.         word_count=0;
  97.     }
  98.  
  99.     else {
  100.  
  101.         for(std::size_t letter=0, length=phrase.length(); letter!=length;++letter){
  102.  
  103.             if((phrase[letter]==' ')&&(phrase[letter-1]!=' ') ){
  104.  
  105.                 ++word_count;
  106.  
  107.             } else {;}
  108.  
  109.         }
  110.     }
  111.     return word_count;
  112. }
  113.  
  114. int char_counts(std::string phrase){
  115.  
  116.     //Function to count the number of characters in a phrase and return the number as an int
  117.  
  118.  
  119.     //trim leading an trailing whitespace
  120.     //Assumes at least one word, othewise function will be stopped at first if statement
  121.     int first=phrase.find_first_not_of(' ');
  122.     int last=phrase.find_last_not_of(' ');
  123.     phrase=phrase.substr(first, last);
  124.  
  125.     int char_count=1;
  126.     std::string alph="ABCDEFGHIJKLMNOPQRSTUVWXYabcdefghijklmnopqrstuvwxyz";
  127.  
  128.     if(phrase.length()<=0){
  129.             //Will quit if only space input or no chars
  130.             char_count=0;
  131.         } else {
  132.  
  133.  
  134.             for(std::size_t letter=0, length=phrase.length();letter!=length;++letter){
  135.  
  136.                 if(alph.find(phrase[letter])!=std::string::npos){
  137.  
  138.                     ++char_count;
  139.                 } else {;}
  140.  
  141.             }
  142.         }
  143.     return char_count;
  144. }
  145.  
  146. void vowel_switch(std::string word){
  147.     std::string new_word=word+"way";
  148.     std::cout<<new_word<<" ";
  149. }
  150.  
  151.  
  152. void consonant_switch(std::string word){
  153.  
  154.     int first_vowel=word.find_first_of("AaEeIiOoUuyY");
  155.     word+=word.substr(0,first_vowel);
  156.     std::string new_word=word.erase(0,first_vowel)+"ay";
  157.     std::cout<<new_word<<" ";
  158.  
  159. }
  160.  
  161. void translatetay(std::string phrase){
  162.  
  163.     int phrase_len=word_counts(phrase), vowel_flag=0;
  164.     std::string new_string="", word="",sentence[phrase_len];
  165.     std::stringstream stream(phrase);
  166.  
  167.     //std::cout<<"The length of the phrase: "<<phrase<<" is "<<phrase_len<<" words."<<std::endl;
  168.  
  169.     std::string vowels="AaEeIiOoUu";
  170.     for(std::size_t i=0;(stream>>word);++i){
  171.  
  172.             sentence[i]=word;
  173.             //std::cout<<"Value of word number "<<i<<" is "<<sentence[i]<<std::endl;
  174.  
  175.             if(vowels.find(word[0])!=std::string::npos){
  176.                         vowel_flag=1;
  177.                     } else {;}
  178.  
  179.             switch(vowel_flag){
  180.  
  181.                                     case 0:
  182.                                             consonant_switch(word);
  183.                                             break;
  184.  
  185.                                     case 1:
  186.  
  187.                                             vowel_switch(word);
  188.                                             break;
  189.  
  190.  
  191.                                     }
  192.  
  193.         }
  194.  
  195.  
  196.  
  197.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement