将 C++ 中的句子转换为 Pig Latin
我是一个初学者,我无法找出一种有效或不太复杂的方法来使这个程序工作。
我需要采用数组形式的句子(通过 cin.getline)并编写一个函数,将其转换为第二个数组中的猪拉丁语。
我已经完成输入和第二个数组集的大小,但我遇到了麻烦,因为我想不出一种方法来找到原始数组中单词的开头,将其转换,然后将其放入新数组,而不创建包含 switch 语句的循环,其中每个 case 都包含一堆嵌套循环。
我必须自己创建所有函数,而不使用字符串库文件。
任何帮助、建议、想法或示例将不胜感激。
编辑:
是的,这是一项大学作业,但我不需要答案,我只需要引导到正确的方向,因为我没有想法。
我一直在尝试使用循环来查找每个单词的开头,然后检查该单词的第一个字母是否是元音或辅音,然后使用循环或嵌套循环来移动内容并将其放入新的单词中数组,但我最终使它变得更加复杂。就像,我使用一个循环来查找单词的开头,然后使用一个巨大的 switch 语句来决定 if 是元音还是辅音,然后我最终得到的每种情况都是某种我不需要覆盖的新嵌套循环其本身稍后会进入更大的循环。
I'm a beginner and I cannot figure out an efficient or not overly complicated way make this program work.
I need to take a sentence in the form of an array (via cin.getline) and write a function that converts it into pig latin in a second array.
I have the input done and the size of the second array set, but I'm having trouble in that I can't think of a way to make find the start of the words in the original array, convert it, and put it into the new array without making a loop that contains a switch statement where each case contains a bunch of nested loops.
I have to create all of my functions myself without using a string library file.
Any help, suggestions, ideas, or examples would be appreciated.
edit:
Yes, this is a university assignment, but I don't need the answer I just need to be steered in the proper direction because I'm out of ideas.
I've been trying to use a loop to find the start of each word and then check whether or not the first letter of the word is a vowel or consonant and then use loop or a nested loop to shift things and put it into the new array but I end up making it more complicated. Like, I use a loop to find the start of a word then a HUGE switch statement to decide whether or not if's a vowel or a consonant and then I end up with each case being some kind of new nested loop that I need to not overwrite itself later in the larger loop.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,一些随机的想法:
将一个单词进行猪拉丁化:如果第一个字母是元音,则微不足道;如果没有,找到第一个元音。将字符串分成两部分;输出第二部分加上第一部分加上“ay”。
要查找辅音,只需测试“不是元音”即可。基本上,您只需要一个
is_vowel()
函数。使用
std::string
。您要做的任何其他事情都不是学习C++。如果您想剧透,请发表评论。
OK, a few random ideas:
To piglatinze a word: if first letter is a vowel, trivial; if not, find the first vowel. Split the string into two parts; output second part plus first part plus "ay".
To find a consonant, just test for "not a vowel". Basically, you only need one
is_vowel()
function.Use
std::string
. Anything else you'd be doing would not be learning C++.Leave a comment if you want spoilers.