I'm writing a pig latin program and am having problems moving onto the next word and printing the words. I have to print from the first vowel onto the end of the word and then from the beginning of the word to the first vowel including the vowel itself.

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

char sentence[81];
char *pointer;
int a = 0;

void find_first_vowel(void)
{
    while (sentence[a] != 'A' && sentence[a] != 'a' &&
           sentence[a] != 'E' && sentence[a] != 'e' &&
           sentence[a] != 'I' && sentence[a] != 'i' &&
           sentence[a] != 'O' && sentence[a] != 'o' &&
           sentence[a] != 'U' && sentence[a] != 'u' &&
           a < strlen(sentence))
           {
                 printf("%c", sentence[a]);
                 a = a + 1;
           }
           return;                                   
}


void main(void)
{
    int j, startword, endword; 
     printf("Welcome to the ultimate pig latin translator\n");
     printf("Please enter a phrase up to 80 characters or 'stop' to end: \n\n");
     
     while(strcmp(sentence, "Stop") != 0 && strcmp(sentence, "STOP") != 0 && strcmp(sentence, "stop") != 0)   
     {
             printf("\nEnter a phrase or 'stop' to end: \n");
             gets(sentence);
             printf("\nThe sentence you inputted is: \n %s \n\n", sentence);
             
             pointer = strtok(sentence, " ");
             while (pointer != NULL)
             {
                  pointer = strtok(NULL, " ");
             }
             
             find_first_vowel();
             printf("%c", pointer); 
             
             for(j=a+1;j<strlen(sentence);j++)
             {  
                  printf("%c", sentence[j]);
             }
             
             for(j=0; j<a; j++)
             {
                  printf("%cay", sentence[j]);
             }
     }
          

     getche();
}

Recommended Answers

All 6 Replies

Oh, so much wrong, it's hard to know where to start. Let's start with this:

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

char sentence[81];
char *pointer;
int a = 0;

// Do not use global variables.  Pass the values needed into the function 
// then return the value you need via the RETURN
void find_first_vowel(void)
{
    while (sentence[a] != 'A' && sentence[a] != 'a' &&
           sentence[a] != 'E' && sentence[a] != 'e' &&
           sentence[a] != 'I' && sentence[a] != 'i' &&
           sentence[a] != 'O' && sentence[a] != 'o' &&
           sentence[a] != 'U' && sentence[a] != 'u' &&
           a < strlen(sentence))
           {
                 printf("%c", sentence[a]);
                 a = a + 1;
           }
           return;                                   
}


void main(void)
{
    int j, startword, endword; 
     printf("Welcome to the ultimate pig latin translator\n");
     printf("Please enter a phrase up to 80 characters or 'stop' to end: \n\n");
 
     // What is the value of sentence at this point in the program?
     while(strcmp(sentence, "Stop") != 0 && strcmp(sentence, "STOP") != 0 && strcmp(sentence, "stop") != 0)   
     {
             printf("\nEnter a phrase or 'stop' to end: \n");
             gets(sentence);            // gets() is a VERY DANGEROUS function.  See link below.
             printf("\nThe sentence you inputted is: \n %s \n\n", sentence);
             
             pointer = strtok(sentence, " ");
             while (pointer != NULL)
             {
                  pointer = strtok(NULL, " ");
             }
             
             find_first_vowel();
             printf("%c", pointer);

Once you get this part working, explain what the program is doing wrong so we know what to look for.

Here's the link I mentioned

Okay so i re-wrote everything and simplified it a lot but my only problem now is that i cannot get the function to start printing from the first vowel on and it instead simply prints from the second letter on.

#include <stdio.h>
  #include <string.h>
 
void printLatinWord(char *);
 
int main(void)
{
      char sentence[80];
      char *pointer;
      int a = 0;
      
     while(strcmp(sentence, "Stop") != 0 && strcmp(sentence, "STOP") != 0 && strcmp(sentence, "stop") != 0)   
     {
      
           printf("\nEnter a phrase:\n");
           gets(sentence);
           printf("\nThe phrase in Pig Latin is:\n");
 
           pointer = strtok(sentence, " ");
 
           while (pointer != NULL)
           {
               if  (sentence[a] != 'A' && sentence[a] != 'a' &&
                    sentence[a] != 'E' && sentence[a] != 'e' &&
                    sentence[a] != 'I' && sentence[a] != 'i' &&
                    sentence[a] != 'O' && sentence[a] != 'o' &&
                    sentence[a] != 'U' && sentence[a] != 'u' &&
                    a < strlen(sentence))
                    {
                         a = a + 1;
                    }
               printf("%s%c%s", pointer + 1, pointer[0], "ay");
               pointer = strtok(NULL, " ");
 
               printf(" ");
           }
 
              printf("\n");
 
       
       }
       
       getche();
 }

So your fix was to get rid of the function and ignore all my other comments.

If you were me, what would you do?

no, I simplified it and the gets command has to be there because we were told that we needed to use it. the function was removed and moved because it was able to work when put in the body of the main function. It helped immensely as now i only cannot start from the first vowel as opposed to not being able to print anything past the first word at all

I played around with it a little bit. I believe something like this will give you the result you're looking for.

char beginning[70] = "";
char end[10] = "";
strncpy(beginning, sentence + a, strlen(sentence) - a);
strncpy(end, sentence, a);
printf("%s%s%s", beginning, end, "ay");

I'm pretty sure you're going to have to make a couple of revisions to your code before that will work, though.

Did you have to use

a = a + 1

?

a++

will be more simple.

commented: So what. If they are equivalent, it really doesn't matter, does it? -4
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.