Going all the way with the whale translation additions

I’m currently taking the C++ course, and for the most part I try to challenge myself to the greatest extent I can bear. I’m on the Whale Talk project, and I’m trying to make the fallowing additions recommended by code academy:

-Add handling for uppercase and mixed case strings.

-Include the punctuation from the original string in the whale translation.

-Make exclamation points double the last vowel in the string.

and this is what i have so far:

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

int main () {

//the parts of a vector are elements,
//starting at number [0] to the computer.
//figure out how to put last vowel where ! is

std::string string;

std::cin >> string;

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

std::vector<char> punct = {'.', ',', ':', ';','?', '(', ')'};

std::vector<char> result;

//nested for loop so that iteration of string1
//determines/controls vowels.

for (int i = 0; i < string.size(); i++){
  for (int j = 0; j < vowels.size(); j++) {
    if (string[i] == vowels[j]){
       result.push_back(string[i]);
       if (string[i] == 'e' || string[i] == 'E' || string[i] == 'u' || string[i] == 'U'){
       result.push_back(string[i]);
       }
  }

 }
  for (int l = 0; l < punct.size(); l++){
    if (string[i] == punct[l]){
      result.push_back(string[i]);
    }
  }

}

for (int k = 0; k < result.size(); k++) {

  std::cout << result[k];

}

}

I even thought it would be interesting to accept input for the translation instead of hard code the string into the program, which was easy. However, I’m having a hard time figuring out how to take an exclamation point and use it to repeat the last vowel.

Thanks!

Maybe if you fleshed out your description of what should happen.

Take an exclamation point? grab it? what?
Would you use an exclamation point to repeat a vowel? How does that work? It’s not a tool that repeats things.

You’re not going to implement something until you’ve decided what the thing at all is!

Once you’ve decided what to do, you can solve small parts.
For example, what is a vowel?
And then, when you have a solution to that question, which one is the last vowel? (if any)
What other problems are there that can be solved before trying to do the whole thing?

Once you’ve solved enough subproblems you might find that the overall thing is trivial by combining the small solutions you’ve made.

Thanks for trying but i’ve been doing that for the past several hours…the problem is that the ! needs to be erased and replaced by a vowel, whereas this program is designed to just keep checking the string that someone inputs.

If there was some way to just make it go backward when it hits an exclamation point, and then print the last vowel, then that would work. I already figured out how to erase the exclamation point with string[i] - 1

It’s an entirely isolated operation, isn’t it? It’s something you could do after everything else. So the design of the program doesn’t matter.

It’s not entirely clear to me how that’s supposed to be interpreted either.
Which exclamation marks do you care about? Any? Only as the last character of the string? One or many?
Would double turn two vowels into four, four into eight? Or would it only repeat a letter.
You mention replacing exclamation marks. Do you mean that:
Yes!
should get turned into:
Yese
?
or maybe:
Yees
or
Yees!
What about a!a!e!?
Is e the last vowel, or do the different exclamation marks have different last vowels?
Doesn’t really matter, but you have to pick something, make a decision.

No it’s not that simple because i don’t know how to make header files and change the capabilities of c++…and i could just forget about it and move on but i’m currently obsessed!

not sure why you’d involve header files.
was there something in particular that I said that seemed impossible?
mostly I’ve been saying that you need to decide on exactly what you mean should happen so that you have a reference when you write code. otherwise you’ll start writing code and find that you don’t know what you mean should happen and uh …

The other thing I’ve been saying is that there are small problems which you can solve individually, to later be combined into a larger thing.

Well. Consider this.

If you’re given a string and an index,
can you find the last vowel that occurs before that index?

You could make a function for that.

int last_vowel_before(int i, std::string s) {
    ...
}

So that would be solving a small part of the problem.

And then, if you had such a function, could you use it to get what you want?
For example, when you encounter an exclamation mark, you could call the function to find the appropriate vowel.

I only mentioned header files because that seems to be a way for people to change what a programming language can do…

and your proposed function illustrates the problem i’m having: what goes between the brackets, the code that runs when the condition is met.

If i could have:

if(string[i] == '!') {
...
}

i wouldn’t even need a function. Another idea i’ve been toying with is somehow making the program check the size of the string (easy) and turning that into a variable so that i can check the last element and somehow turn it into a vowel…

but then i still haven’t gotten any closer. I don’t know how to use functions even though i know what they are.

No you don’t need a function for that. The point is to take a small part of the problem, solve it, then look at what’s left, maybe solve a few more problems, and then when enough problems are solved you can implement the thing you meant to do.

As opposed to: I have no idea how to do this. Which is really just saying that no approach was made.

Figuring out where the previous vowel (if any) is, is a part of the problem, and after having solved it, what remains will be easier.


nothing stopping you.
or if you find that something is stopping you, identify what and work on getting around it

which would be yet another problem to solve.
given a string, a location, a letter … how do you put that letter in that location of the string?
it’s a much smaller problem than the overall thing, but you need it, and you can solve it in isolation from the rest.

so some problems to solve are…
where is the exclamation mark(s)
where are the relevant vowels, if any
how does one modify a string

is there anything left after that, and if so, what? if not, use them together.

wow, this interesting. “No approach was made”, so in other words, i put all this work into solving this problem, you make tons of posts, yet none of them answer my question. So do you know what code to put into the brackets?

So why? I also did a ton of work to try to find if someone answered this question, and i found that…no, with my google skills (which i admit are not miraculous), all the code on github just illustrates that the original question was answered but not the extra credit…

so i digress…since no “approach was made”, and yes this is still an intro class, do you actually know the answer to my original question, or shall we just talk in (i wanted to say “code”…) circles? In other words: what’s the answer? What code gets triggered to take back the original char and turn it into my vector of vowels?

i mean, this is basically a main staple of the thread, yet you also don’t seem to know the answer…

yeah i will look into the last question, how does one modify a string? Something I just wasn’t talking about before, thank you.

I guess what I’m trying to say is…yeah, lot’s of words in this thread, sounds like my motivations for posting this, but code? Is there a way to modify what i posted, or is there just a better way to perform what the program was intended for? That is all, thank you…

After a lot of thinking and taking a break i was able to figure it out. Getting the program to replace an exclamation point with the last vowel actually just involves making another loop like so, which checks the restult, pops the ! back, and stores the second to last vowel in the result vector for printing in the final loop:

for (int k = 0; k < result.size(); k++){
   for (int p = 0; p < punct.size(); p++){
        if (result[k] == '!'){
           result.pop_back();
           result.push_back(result[k-2]);
        }

also, i was noticing a problem with the way i set it up before, where any sort of whitespace in the string inputted would stop the rest of the sentence from getting read. Here is a full whale talk translator, the only thing missing is being able to transform the ! into a vowel earlier in the sentence, which i think would be pretty neat since who says that somone can’t get excited in the middle of a sentence?!:

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

int main () {


std::string string;

std::getline (std::cin, string);

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

std::vector<char> punct = {'.', ',', ':', ';','?', '(', ')', '!'};

std::vector<char> result;

for (int i = 0; i < string.size(); i++){
  for (int j = 0; j < vowels.size(); j++) {
    if (string[i] == vowels[j]){
        result.push_back(string[i]);
        if (string[i] == 'e' || string[i] == 'E' || string[i] == 'u' || string[i] == 'U'){
        result.push_back(string[i]);
       }
       }
}
}
for (int i = 0; i < string.size(); i++){
   for (int l = 0; l < punct.size(); l++){
    if (string[i] == punct[l]){
      result.push_back(string[i]);
      }
}
}

for (int k = 0; k < result.size(); k++){
   for (int p = 0; p < punct.size(); p++){
        if (result[k] == '!'){
           result.pop_back();
           result.push_back(result[k-2]);
        }
}
}


for (int k = 0; k < result.size(); k++) {

  std::cout << result[k];

}

}