Pig Latin

I'm having trouble figuring out how to test the first letter more than once for a pig latin program. Here's what I have so far:

if (word[0] != 'a')
{
char temp = word[0];
for (int j = 1; j < len; j++)
{
word[j - 1] = word[j];
}
word[len - 1] = temp;
}

How do I loop this until it finds a letter that's not a vowel?
I've tried a while loop, it basically stops the program, don't understand it.
elaborate what you are trying to say and what do you actually want?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//you want this in loop:
//may be nested loop help.

for(int i=0;i<len; ++i)
{
   if (word[i] != 'a') //you said all vowel so here this condition is 
   {                          //applicable only for 'a'. You gotta change it too.
      char temp = word[0];
      for (int j = i; j < len - i; j++)
      {
          word[j - 1] = word[j];
       }
       word[len - 1] = temp;
    }
    else
        break;
}
Last edited on
Take the vowel checking logic and put it in its own function. You can call the function in the while loop and read the code at a more conceptual level, instead of polluting your code with mostly redundant and wordy checks:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>

bool isVowel(char c)
{
    return (c == 'a' || c == 'A' ||
            c == 'e' || c == 'E' ||
            c == 'i' || c == 'I' ||
            c == 'o' || c == 'O' ||
            c == 'u' || c == 'U' );
}

int main()
{
    char word[] = "programming";
    int len = 11;
    // The actual length of the string is 12 when considering the null terminator, but
    // I don't want to consider that position at all in the following loop. The loop works
    // by cycling consonants to the back of the word until the first vowel is reached. I
    // never want to overwrite the null terminator, so I'll only consider the indicies that
    // come before it.

    while ( !isVowel(word[0]) ) //i.e. "While the first character in word is not a vowel..."
    {
        char temp = word[0];
        for (int j = 1; j < len; j++)
        {
            word[j - 1] = word[j];
        }
        word[len - 1] = temp;
    }

    std::cout << word << std::endl;
    return 0;
}

Last edited on
Topic archived. No new replies allowed.