1

I have breakdown the problem into several step of mine to translate into pig latin

1.Loop Locate the space in the array

- if now(arr) is not space and not null then keep searching(+1 arr)
- if null then break (mean that go to next step 2)

2.start copying the value to pig latin variable

-store start to temp
-start copying value from start until locatespace-2 to pig_latin
-add temp to pig_latin[index]
-add char 'a' and 'y' and to piglatin[index+1] and piglatin[locatespace+2]
-add space to piglatin[index+3]

3.Start is locatespace+1(which is after space)

//end of my algorithm

So, I've Updated my program and succesfully do this with my own...this is the last problem, and it's related to null

void pigLatin(char english[])
{
    int locateSpace = 0;
    int index = 0;
    int start = 0;
    int i = 0;
    char piglatin[80];

    for (; english[i] != '\0'; i++, locateSpace++)
    {
        char temp = english[i];

        for (; english[i] != ' ' && english[i] != '\0'; i++) {
            locateSpace++;
        }

        for (; start < locateSpace - 1 ; start++ , index++) {
            piglatin[index] = english[start +1];
        }

        piglatin[index] = temp;
        piglatin[index+1] = 'a';
        piglatin[index+2] = 'y';
        piglatin[index + 3] = ' ';

        index += 4;
        start = locateSpace + 1;
    }
    piglatin[index] = '\0';
    printf("\n%s", piglatin);
}

So, my problem is

  • How to insert null to end piglatin variable with?it seems like i don't have idea with this

I've trying a lot of ways but still cant figure it out, so any idea?

Kevin
  • 43
  • 3
  • 10
  • See the `english++` in the for loop itself? That skips over the space, but it doesn't increment locateSpace which I gather is supposed the current position. – user253751 May 12 '16 at 02:52
  • awesome.....be right back after updating to next step – Kevin May 12 '16 at 02:56
  • Look up `strchr`. You can use this to find spaces easily and quickly. – John Zwinck May 12 '16 at 03:15
  • @immibis CodeUpdated, but it seems i'm stuck on step 2 , seems like something wrong, but i can't figure it out – Kevin May 12 '16 at 04:12
  • @John Zwinck Actually i'm not allowed to use function for do this, so i need to do this in hard way , but i'm fine because it's challenging for me. but the problem I'm stuck on step 2, which is i fail to copy the value to pig latin – Kevin May 12 '16 at 04:13

3 Answers3

1

You may be reading a little more into it than is necessary. When working down the english string, you may find it easier to simply use a pointer to the current position in each string rather than trying to keep track of the array index (e.g. start = locateSpace + 1;). If you set pointer e to english and pointer p to piglatin, you can work down your english string and as you add characters to piglatin, simply advance each pointer as you go.

The following is a short example. You can easily convert from the pointer use back to array indexes if you must, but you may find advancing a pointer a more natural way that keeping a count of characters. For example below the constant MAXC can be your 40 (but you might as well make it handle a long line):

void piglatin (char *english)
{
    char piglatin[MAXC] = "";
    char *e = english, *p = piglatin;
    int c = 0, first = 1;

    /* for each char in english and len < MAXC - 2 */
    for (; *e && e - english + 2 < MAXC; e++) {
        if (('A' <= *e && *e < 'Z') || ('a' <= *e && *e < 'z')) {
            if (first == 1) {       /* if first char in word */
                c = *e, first = 0;  /* save, unset flag      */
                continue;           /* get next char         */
            }
            else 
                *p++ = *e;          /* save char in piglatin */
        }
        else if (*e == ' ') {       /* if space, add c+'ay ' */
            *p++ = c, *p++ = 'a', *p++ = 'y', *p++ = *e;
            first = 1;              /* reset first flag  */
        }
    }   /*  add c+'ay ' for last word and print both */
    *p++ = c, *p++ = 'a', *p++ = 'y', *p++ = *e, *p = 0;
    printf (" english  : %s\n piglatin : %s\n", english, piglatin);
}

(note: the code currently ignores punctuation (e.g. any ,;...) and does not worry about changing upper-case to lower-case, etc..)

A quick example using the function with your "Hello, World" example is:

#include <stdio.h>

enum { MAXC = 256 };

void piglatin (char *e);

int main (void) {

    char english[MAXC] = "Hello, World";

    piglatin (english);

    return 0;
}

Example Use/Output

$ ./bin/str_eng2piglatin
 english  : Hello, World
 piglatin : elloHay orldWay

You can add to it to handle more aspects of piglatin as you go (e.g. no carry of 1st char if it is a vowel, and so on) Look it over and let me know if you have any questions.

Your Indexed Version

You were so close with your last version. Just a couple more conditionals and you were home free. Look at the code that follows piglatin[index + 1] = 'y'; and the removal of && english[i + 1] != '\0':

void pigLatin (char english[])
{
    int index = 0;
    char piglatin[80];

    for (int i = 0, start = 0 , locateSpace = 0; english[i]; i++, locateSpace++)
    {
        char temp = english[i];

        for (;english[i] != ' ' && english[i]; locateSpace++, i++) {}

        for (; start < locateSpace ; start++, index++)
            piglatin[index] = english[start +1];

        piglatin[index - 1] = temp; 
        piglatin[index] = 'a'; 
        piglatin[index + 1] = 'y';
        if (english[i] == ' ')
            piglatin[index + 2] = ' ', index += 3;
        else
            index += 2;

        start = locateSpace + 1;
    }
    piglatin[index] = 0;        /* nul-terminate string */
    printf("\n'%s'\n", piglatin);
}
David C. Rankin
  • 81,885
  • 6
  • 58
  • 85
  • Actually i was doing it with pointer, but at some point i was thinking it should be look better and cleaner using array syntax( actually this is my interview question), so i decided to use array syntax, and i want to ask ....whats wrong with my null at the end of variable?i can't figure it out yet – Kevin May 12 '16 at 06:24
  • I don't think the problem is with your *nul-termination*, I think the problem is the fact you tack an extra `space` on the end of the string before the *nul-termination*. Try your function with `printf("\n'%s'\n", piglatin);` `0` and `'\0'` are equivalent. – David C. Rankin May 12 '16 at 06:53
  • found the problem, it's because of first loop, it seems like my "for" for checking the location is broken.....but i don't understand why it's broken, the for statement seems legit for me – Kevin May 12 '16 at 07:05
  • I'm looking at all 3 of your loops. Technically they are fine. How are you saying the first loop is broken? I run your function and get `'ello,Hay orldWay '`? Which is doing what you are telling it to do. You are not checking `a-zA-Z`, so the comma will be considered part of `Hello`, which is fine for purposes here. What do you think is broken? – David C. Rankin May 12 '16 at 07:16
  • well i found the mistake, in second loop of for, i should check if the next array of english is null or not, so if null i i wont increment locatespace because when i done the second loop and back to first loop, the first loop should increment the locatespace, that why if i dont check the next element of english is null, at the end it will return locatespace+1(which is null +1) and ended up taking space just like you said – Kevin May 12 '16 at 07:23
  • Good deal, glad you got it figured out. That is another reason I prefer the pointer approach, it takes less Aspirin than keeping track of `i`, `start`, `locatespace` and how they are synced between multiple loops. (especially the older I get `:)` – David C. Rankin May 12 '16 at 07:28
  • well, I used to be hate programming because of my college, but somehow when i try to learn c + front end from 0,I feel like need to get programming related job especially in back-end, really interested to solve problem.Well great idea to help me figure it out :D – Kevin May 12 '16 at 07:36
  • C is a fantastic language. The learning curve is steeper than most languages, but it is well worth the time. Nothing else will give you the machine level control you get with C (aside from assembly). It is a good game of chess, and takes about as long to master. – David C. Rankin May 12 '16 at 07:43
0
void pigLatin(char english[])
{
    int index = 0;
    char piglatin[80];

    for (int i = 0, start = 0 , locateSpace = 0; english[i] != '\0'; i++, locateSpace++)
    {
        char temp = english[i];

        for (;english[i] != ' ' && english != '\0' && english[i + 1] != '\0';) {
            locateSpace++;
            i++;
        }

        for (; start < locateSpace ; start++ , index++) {
            piglatin[index] = english[start +1];
        }

        piglatin[index - 1] = temp; 
        piglatin[index] = 'a'; 
        piglatin[index + 1] = 'y';
        piglatin[index + 2] = ' ';

        index += 3;
        start = locateSpace + 1;
    }
    piglatin[index] = '\0';//close the end of string 
}
Kevin
  • 43
  • 3
  • 10
  • Your close, but what happened to the `d` at the end of `World`? It prints as `'ello,Hay orlWay '` now?? – David C. Rankin May 12 '16 at 07:42
  • @David C. Rankin updated and solved! didnt realize it till read your comment :D – Kevin May 12 '16 at 07:53
  • Give it the old college try one-more-time. Still `'ello,Hay orlWay '`. (I provided an edit to my answer that might help `:)` – David C. Rankin May 12 '16 at 08:03
  • @DavidC.Rankin i already submitted my answer as my interview question hahaha......didnt try that before tho....this is really fun :D wont never forget this mistake – Kevin May 12 '16 at 08:58
0
$ ./a.out
     pig -> igpay
   latin -> atinlay
  banana -> ananabay
   happy -> appyhay
    duck -> uckday
      me -> emay
     eat -> eatay
  omelet -> omeletay
     are -> areay
     egg -> eggay
 explain -> explainay
  always -> alwaysay
  string -> tringsay
  string -> ringstay
  string -> ingstray

// Pig Latin
// Pig Latin is a game of alterations played on the English language game.
// To create the Pig Latin form of an English word the initial consonant
// sound is transposed to the end of the word and an at is affixed.
// (Ex: "banana" would yield "anana-bay").

// Just work for lowercase characters

#include <stdio.h>
#include <string.h>
#include <stdbool.h>


bool prefix(const char *pre, const char *str)
{
    return strncmp(pre, str, strlen(pre)) == 0;
}

char *
get_consonant_cluster(char *str, char *consonant_clusters[], size_t cclen)
{
    char *consonant_cluster = NULL;
    size_t i;

    for (i = 0; i < cclen; i++)
    {
        if (prefix(consonant_clusters[i], str))
        {
            consonant_cluster = consonant_clusters[i];
            break;            
        }
    }

    return consonant_cluster;
}

char *
get_consonant_cluster_2c(char *str)
{
    char *consonant_clusters[] = {
        "bl", "br", "ch", "cl", "cr", "dr", "fl", "fr",
        "gl", "gr", "pl", "pr", "sc", "sh", "sk", "sl",
        "sm", "sn", "sp", "st", "sw", "th", "tr", "tw",
        "wh", "wr",
    };
    size_t cclen = sizeof(consonant_clusters) / sizeof(consonant_clusters[0]);

    return get_consonant_cluster(
        str,
        consonant_clusters,
        sizeof(consonant_clusters) / sizeof(consonant_clusters[0])
    );
}

char *
get_consonant_cluster_3c(char *str)
{

    char *consonant_clusters[] = {
        "sch", "scr", "shr",
        "sph", "spl", "spr", 
        "squ", "str", "thre"
    };

    return get_consonant_cluster(
        str,
        consonant_clusters,
        sizeof(consonant_clusters) / sizeof(consonant_clusters[0])
    );
}

bool
is_vowel(char ch)
{
    switch(ch)
    {
        case 'a':
        case 'o':
        case 'e':
        case 'i':
        case 'u':
            return true;
        default:
            return false;
    }
}

void
pig_latin(char *str)
{
    char *cc2ptr = NULL;
    char *cc3ptr = NULL;
    int cclen;

    if (is_vowel(*str))
    {
        printf("%8s -> %s%s\n", str, str, "ay");
    } else {

        cc2ptr = get_consonant_cluster_2c(str);
        cc3ptr = get_consonant_cluster_3c(str);

        // string -> tring + s + ay
        {
            printf("%8s -> %s%c%s\n", str, str + 1, *str, "ay");
        }

        // string -> ring + st + ay
        if (cc2ptr != NULL)  // consonant clusters (multiple consonants that form one sound)
        {
            printf("%8s -> %s%s%s\n", str, str + strlen(cc2ptr), cc2ptr, "ay");
        }

        // string -> ing + str + ay
        if (cc3ptr != NULL)
        {
            printf("%8s -> %s%s%s\n", str, str + strlen(cc3ptr), cc3ptr, "ay");
        }
    }
}

int
main(void)
{
    pig_latin("pig");
    pig_latin("latin");
    pig_latin("banana");
    pig_latin("happy");
    pig_latin("duck");
    pig_latin("me");

    pig_latin("eat");
    pig_latin("omelet");
    pig_latin("are");
    pig_latin("egg");
    pig_latin("explain");
    pig_latin("always");

    pig_latin("string");

    return 0;
}

// https://en.wikipedia.org/wiki/Pig_Latin
// https://en.wikipedia.org/wiki/Vowel
// https://simple.wikipedia.org/wiki/Vowel
// https://en.wikipedia.org/wiki/Consonant_cluster
// http://www.enchantedlearning.com/consonantblends/
// https://stackoverflow.com/questions/4770985/how-to-check-if-a-string-starts-with-another-string-in-c
// https://stackoverflow.com/questions/1088622/how-do-i-create-an-array-of-strings-in-c
debug
  • 991
  • 10
  • 14