Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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.
C#
#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;
}

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.
Posted
Updated 26-Apr-15 14:30pm
v4
Comments
PIEBALDconsult 26-Apr-15 21:26pm    
I think your definition of pig-latin may be flawed. How do you intend to handle words like "use"?
Sergey Alexandrovich Kryukov 26-Apr-15 21:39pm    
How, how... For example: "useless". Good word for the case, isn't it? :-)
I guess this is just an exercises, but even for the exercises, the rules of the games look undefined.

And "I want to use only arrays and control flows (no functions)" make it "pic C", not C, but also nothing defined or reasonable.

—SA
nv3 27-Apr-15 4:44am    
That looks like a nice exercise to demonstrate the importance of functions. But do you really want us to do your homework? You won't learn anything if we do it for you.

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
 
Share this answer
 
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.
 
Share this answer
 
v2
Comments
PIEBALDconsult 27-Apr-15 10:51am    
Poor use of recursion.
W Balboos, GHB 27-Apr-15 10:54am    
There are those who would argue that there's never a good use for recursion.

It could have been in while-loop (so I must accept your critique) but it's not nearly as much fun.

PIEBALDconsult 27-Apr-15 10:56am    
I agree.
Now to try it with Regular Expressions...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900