Changing Vowels to X - c++ [closed]
Want to improve this question? Update the question so i开发者_如何转开发t focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this questionI have an assignment where I need to take a string and replace each vowel with the letter 'x'. For example "real"
would be replaced with "rxxl"
.
Since this is homework I don't want to provide the answer directly, but instead I will give you some suggestions. There are going to be two major aspects to this process:
- a bit of code to change a single
char
from a vowel to x. - a bit of code to loop through every
char
in the string and apply the bit of code from above.
Try to take the problem and decompose it into smaller parts, like above -- and tackle those smaller parts on their own until you can see how they fit back into the larger picture. If even the two smaller parts I've suggested are too difficult for you, try breaking them down even further. For example, perhaps you are unfamiliar with how to change any character in a string to anything else, and you should try to tackle that first.
Steps to solve:
- How do you iterate (go over) elements in a string, so that you can see what each character contains? You need some sort of loop and a way to see what the string contains at each position.
- How do you write a new string (or overwrite characters in a given string)?
- How do you check that a given character is a vowel? (consider using switch)
That should set you going.
A basic algorithm is to split it up into three parts and first solve each separately:
- Iterate over the characters with a
for
loop. - Write a function to determine if a character is a vowel.
- Change a character in a string to another character.
However this simplified algorithm strictly speaking doesn't always work. Before solving this task you should try to learn a little about what a vowel is. For example in English the following letters almost always produce vowel sounds: a, e, i, o, u. The letter y often produces a vowel (e.g. in "myth" or "happy"), but not always. There are also other letters that produce vowel sounds more rarely.
You should also also know that almost all teachers (and probably more than half of professional programmers) believe that there only five vowels - a, e, i, o and u. As such I'd advise you to keep it simple and use the five vowel interpretation in order to avoid being marked down. But write a note in the source code that you are aware that there are some letters that are incorrectly classified and that you have chosen not to fix it, so that you at least have proven that you are aware of the issue.
In my opinion this is a terrible question to set for beginner level students as it either requires the student to less time on programming and more time on learning about the intricacies and inconsistencies of the English language. Or else it encourages coding without fully understand the domain. A better example would be:
Replace the letters:
- a,e,i,o,u with x
- A,E,I,O,U with X
It would be a good idea to put your interpretation of the question in your solution to ensure that there is no misunderstanding.
精彩评论