从英语到猪拉丁语 [英] From English to Pig-Latin

查看:76
本文介绍了从英语到猪拉丁语的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Pig-Latin规则:

1.以元音开头的单词 - 在单词的末尾添加'way'。

Ex:egg - > eggway,airplane - >航空飞行。

2.以辅音开头的一个单词 - 所有辅音直到第一个元音移到单词的末尾,并在单词的末尾添加'ay'。

Ex:pig - > igpay,冠 - > owncray。

我只想使用数组和控制流(没有函数)。

我已经完成了这段代码,但卡在我需要操作的地方分开句子中的单词将它们改为Pig-Latin。我做了或多或少的'介绍',但我不知道什么是必要的,什么不是。

Pig-Latin rules:
1. A word starting in a vowel - 'way' is added in the end of the word.
Ex: egg --> eggway, aeroplane --> aeroplaneway.
2. A word starting in a consonant - all the consonants until the first vowel are moved to the end of the word and 'ay' is added in the end of the word.
Ex: pig --> igpay, crown--> owncray.
I want to use only arrays and control flows (no functions).
I've done this code but got stuck in the place that I need to manipulate seperate words within the sentence to change them to Pig-Latin. I did more or less the 'intro' but I don't know what's necessary and what's not.

#include <stdio.h>
#define IN  1 /* Inside a word */
#define OUT 0 /* Outside a word */

int main()
{
    char lan, c, sen[], word[];
    int  i, len, nw = 0;
    state = OUT;

    printf("Please choose:");
    printf("1: Pig-Latin");
    printf("2: B-Language");
    printf("0: Finish\n");

    do
    {
        scanf("%c", &lan);
        if (lan == '1') /* Pig-Latin */
        {
            printf("Please enter a sentence.\n");
            for (len = 0; (c = getchar()) != '\n'; len++)
            {
                sen[len] = c; /* Sentence length */
            }
            sen[len] = '\0';
            for (i = 0; i <= len; i++)
            {
                if (sen[i] == ' ')
                    state = OUT;
                else if (state == OUT)
                {
                    state = IN;
                    nw++; /* New words counter */
                    for (j = 0; ???; j++)
                    {
                        switch(sen[i]): /* Sentence starts in a vowel */
                            case 'a':
                            case 'e':
                            case 'i':
                            case 'o':
                            case 'u':
                                sen[i to j] = sen[i to j]'way'; /* Prototype */
                                break;
                    }
                }
                else /* Sentence starts in a consonant */
            }
        }
    }while(lan - '0')
    return 0;
}



忽略其他语言。在'切换'条件之前的'for'循环中,我把???因为我认为需要成为一个条件,所以我能够分开处理每个单词。


Ignore the other languages. In the 'for' loop before the 'switch' condition I put ??? because I think there's need to be a condition so I would be able to work on every word seperatly.

推荐答案

首先,把句子分成单词,通过查找空格,或使用 strtok 函数



将每个单词推入数组



然后遍历那个数组,'把'字化,把结果推到同一个数组,或另一个数组



然后走过修改后的阵列,打印每个成员后面有一个空格



数组是令人讨厌的,它们必须被预先设定(除非你正在做它们是动态的)和字符串数组甚至更令人讨厌



你可能有更多的运气使用 std :: vector [ ^ ]的std::string [ ^ ] s
first, take the sentence and split it up into words, either by looking for spaces, or using the strtok function

Push each of those words into an array

Then walk through that array, 'pig'ify the word, and shove the result into the same array, or another one

Then walk thru the modified array printing each member with a space after it

array's are irksome, they have to be presized (unless you're doing them dynamically) and arrays of strings are more even irksome

you might have more luck using std::vector[^]s of std::string[^]s


我实际上在其中一个(大约20年前)在命令行界面上工作。



有许多猪拉丁方言。通用性是如何在第一个元音之前递送任意数量的辅音?



我解决了这个问题,处理了从无用到心理的任何事情(及以后)。



创建一个递归函数,它可以完成两件事:

如果有一个辅音,请将它移到最后字符串和调用本身。

如果有元音,请附加方言的结尾并启动返回链。



提示(警告):in案例输入是意外的所有辅音,你必须防止失控的递归。这并不难,但我把它留给你的想象力。当然,您需要将文本输入转换为单个单词。







注意:Piglatin to English是一个更难的问题。
I actually made one of these (about 20 years ago) working on a command-line interface.

There are many dialects of pig-latin. The commonality is "how do you hand an arbitrary number of consonants preceding the first vowel?"

I solved this in a manner which handled anything from 'useless through psychological' (and beyond).

Create a recursive function that does two things:
If there's a consonant, move it to the end of the string and call itself.
If there's a vowel, append your dialect's ending and start the return chain.

Hint (warning): in case input is accidentally all consonants you have to prevent a runaway recursion. That's not difficult, but I leave it to your imagination. You will, of course, need to convert your text input into individual words.



Note: Piglatin to English is a more difficult problem.


这篇关于从英语到猪拉丁语的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆