Expanded Whale Talk Project with the suggested hints

I was wondering if anyone was able to complete the full version of the Whale Talk program with all of the hints they offer in step 10?

This is what I had so far:

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

int main() {

  // Whale, whale, whale.
  // What have we got here?
  std::string input;

  system("clear");
  //Header
  std::cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
  std::cout << " Whale Speak Translator 3000\n";
  std::cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n";

  cout << "Word / phrase: ";
  getline(cin, input);
  std::cout << "\n";

  //Convert the string into lowercase
  for(int b = 0; b <= input.size(); b++) { 
      /* Here we are performing a check so that only those 
       * characters gets converted into lowercase that are 
       * in uppercase. 
       * ASCII value of A to Z(uppercase chars) ranges from 65 to 92 
       */ 
      if(input[b] >= 65 && input[b] <= 92) {
	  
        input[b] += 32;
      
      }
   }

  std::vector<char> vowels = {'a', 'e', 'i', 'o', 'u'};
  std::vector<char> result;

  //Uncomment below line for testing
  //std::cout << input << "\n";

  for (int i = 0; i < input.size(); i++) {

    //Checks for vowels
    for (int x = 0; x < vowels.size(); x++) {

      if (input[i] == vowels[x]) {

        result.push_back(input[i]);

      }
    }

    //Doubles the e's, and u's
    if (input[i] == 'e' || input[i] == 'u') {

      result.push_back(input[i]);

    } 
  }

  //Prints the full result
  for (int a = 0; a < result.size(); a++) {

    std::cout << result[a];

  }

  std::cout << "\n";

  return 0;
}

I have the program set to handle uppercase letters, but can’t seem to get the last two points.

For the punctuation hint, my solution was very crude, I think, but it does the work:

if (input[i] == ',' || input[i] == '?' || input[i] == '!') {
         result.push_back(input[i]);
   }

I saw a solution from another user that was much more elegant: they defined punctuation makers as another vector and followed the same steps in comparing the original string to the vowel vector.

Now the last part about the “!” was more complicated. The simplest solution I was able to come up after some research with was:

if (input[i] == '!') {
  result.push_back(result.back());
}

.back() returns the last element in the vector, so that way you’re including the last element again. It’s important though that this part comes before the punctuation rule that I mentioned first so that it won’t duplicate punctuation marks as well.

Both go inisde the outer loop. That way, you can guarantee you’ll only double vowels and not consonants!

My full code:

#include <iostream>
#include <vector>
#include <string>

int main() {

  // Whale, whale, whale.
  // What have we got here?

  std::string input = "hI, human!";

  std::vector<char> vowels = {'a', 'A', 'e', 'E','i','I','o','O','u','U'};
  std::vector<char> result;

  for (int i = 0; i < input.size(); i++) {

    for (int j = 0; j < vowels.size(); j++) {

      if (input[i] == vowels[j]) {
        result.push_back(input[i]);
       }
       } 

    
 if (input[i] == 'e' || input[i] == 'u') {
       result.push_back(input[i]); 
      }

if (input[i] == '!') {
  result.push_back(result.back());
}

 if (input[i] == ',' || input[i] == '?' || input[i] == '!') {
         result.push_back(input[i]);
   }

}

for (int k = 0; k < result.size(); k++) {
  std::cout << result[k];
}

std::cout << "\n";


}
1 Like