Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //============================================================================
- // Name : MachineLearningProblem6.cpp
- // Author :
- // Version :
- // Copyright : Your copyright notice
- // Description : Hello World in C++, Ansi-style
- //============================================================================
- #include <iostream>
- #include <string>
- #include <sstream>
- int vowel_counts(std::string);
- int word_counts(std::string);
- int char_counts(std::string);
- void translatetay(std::string);
- void vowel_switch(std::string word);
- void consonant_switch(std::string word);
- int main() {
- ///Part 1
- std::string phrase;
- int vowels=0, words=0,chars=0;
- //Input words
- std::cout<<"Input a word phrase."<<std::endl;
- std::getline(std::cin, phrase);
- //std::cout<<phrase<<std::endl;
- //Initial functions for part one for word,vowel and character counters
- vowels=vowel_counts(phrase);
- std::cout<<"There are "<<vowels<<" vowels in the phrase "<<phrase<<std::endl;
- words=word_counts(phrase);
- std::cout<<"There are "<<words<<" words in the phrase "<<phrase<<std::endl;
- chars=char_counts(phrase);
- std::cout<<"There are "<<chars<<" characters in the phrase "<<phrase<<std::endl;
- //Part 2
- ///Function to translate and output words
- translatetay(phrase);
- }
- int vowel_counts(std::string phrase){
- //Function to count the number of vowels in a phrase and return the number as an int
- int vowel_count=0;
- std::string vowels="AaEeIiOoUu";
- //Does not include Y in vowels
- int first=phrase.find_first_not_of(' ');
- int last=phrase.find_last_not_of(' ');
- phrase=phrase.substr(first, last);
- if(phrase.length()<=0){
- //Will quit if only space input or no chars
- vowel_count=0;
- } else {
- for(std::size_t letter=0, length=phrase.length();letter!=length;++letter){
- if(vowels.find(phrase[letter])!=std::string::npos){
- vowel_count++;
- } else {;}
- }
- }
- return vowel_count;
- }
- int word_counts(std::string phrase){
- //Function to count the number of words in a phrase and return the number as an int
- //Assumes at least one word, othewise function will be stopped at first if statement
- int word_count=1;
- //trim leading all trailing whitespace
- //trim leading an trailing whitespace
- //Assumes at least one word, othewise function will be stopped at first if statement
- int first=phrase.find_first_not_of(' ');
- int last=phrase.find_last_not_of(' ');
- phrase=phrase.substr(first, last);
- if(phrase.length()<=0){
- //Will quit if only space input or no chars
- word_count=0;
- }
- else {
- for(std::size_t letter=0, length=phrase.length(); letter!=length;++letter){
- if((phrase[letter]==' ')&&(phrase[letter-1]!=' ') ){
- ++word_count;
- } else {;}
- }
- }
- return word_count;
- }
- int char_counts(std::string phrase){
- //Function to count the number of characters in a phrase and return the number as an int
- //trim leading an trailing whitespace
- //Assumes at least one word, othewise function will be stopped at first if statement
- int first=phrase.find_first_not_of(' ');
- int last=phrase.find_last_not_of(' ');
- phrase=phrase.substr(first, last);
- int char_count=1;
- std::string alph="ABCDEFGHIJKLMNOPQRSTUVWXYabcdefghijklmnopqrstuvwxyz";
- if(phrase.length()<=0){
- //Will quit if only space input or no chars
- char_count=0;
- } else {
- for(std::size_t letter=0, length=phrase.length();letter!=length;++letter){
- if(alph.find(phrase[letter])!=std::string::npos){
- ++char_count;
- } else {;}
- }
- }
- return char_count;
- }
- void vowel_switch(std::string word){
- std::string new_word=word+"way";
- std::cout<<new_word<<" ";
- }
- void consonant_switch(std::string word){
- int first_vowel=word.find_first_of("AaEeIiOoUuyY");
- word+=word.substr(0,first_vowel);
- std::string new_word=word.erase(0,first_vowel)+"ay";
- std::cout<<new_word<<" ";
- }
- void translatetay(std::string phrase){
- int phrase_len=word_counts(phrase), vowel_flag=0;
- std::string new_string="", word="",sentence[phrase_len];
- std::stringstream stream(phrase);
- //std::cout<<"The length of the phrase: "<<phrase<<" is "<<phrase_len<<" words."<<std::endl;
- std::string vowels="AaEeIiOoUu";
- for(std::size_t i=0;(stream>>word);++i){
- sentence[i]=word;
- //std::cout<<"Value of word number "<<i<<" is "<<sentence[i]<<std::endl;
- if(vowels.find(word[0])!=std::string::npos){
- vowel_flag=1;
- } else {;}
- switch(vowel_flag){
- case 0:
- consonant_switch(word);
- break;
- case 1:
- vowel_switch(word);
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement