i need to store the piglatin words returned by each function into one string so that I can then check it for lowercase or uppercase words and change as required. I can't seem to get the return 'temp' to store into a string in main. Also, when printed out, the last 2 or 3 letters move to the next line, why is this?

Here is my code thus far: must enter info in all CAPS for now.

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


char * vowel_rule(char *);
char * two_letter(char *);
char * last_rule(char *);
void rules();


int main()
{

    rules();


    return(0);
}



void rules()
{
    char str[80];
    char english[80];
    int length,a,b,i;

    i=0;
    a=b=0;


    printf("Please enter a phrase to be translated to piglatin: ");
    fgets(str,79,stdin);
    length = strlen(str);

    printf("\nThis is your translated sentence: ");

    while(a<=length)
    {
        english[b]=str[a];

    if(english[b]==' '||english[b]=='\0')
    {
      english[b]='\0';
      b=0;

      if(english[0]=='A'||english[0]=='E'||english[0]=='I'||english[0]=='O'||english[0]=='U'||english[0]=='a'||english[0]=='e'||english[0]=='i'||english[0]=='o'||english[0]=='u')
      {   

        printf(" %s",vowel_rule(english));

      }

      else if(english[0]=='T'&&english[1]=='H'||english[0]=='C'&&english[1]=='H'||english[0]=='S'&&english[1]=='H'||english[0]=='P'&&english[1]=='H'||english[0]=='W'&&english[1]=='H'||
              english[0]=='t'&&english[1]=='h'||english[0]=='c'&&english[1]=='h'||english[0]=='s'&&english[1]=='h'||english[0]=='p'&&english[1]=='h'||english[0]=='w'&&english[1]=='h'||english[0]=='Q'&&english[1]=='U'||english[0]=='q'&&english[1]=='u')
      {
        printf(" %s",two_letter(english));
      }

      else
      {
        printf(" %s",last_rule(english));
      }
    }
    else
    {
        b++;
    }

        a++;
  }

}





char * vowel_rule(char *str)
{
    static char temp[80];
    memset(temp,'\0',80);

    strcpy(temp,str);      //copy word starting from second character to temp
    temp[strlen(temp)+1]= '\0';   // Null terminate temp
    strcat(temp,"AY");     // append "ay" to the string.

    return temp;
}




char * two_letter(char *str)
{
    static char temp[80];
    memset(temp,'\0',80);
    char first,append[]="H";

    first=str[0];
    if(str[1]=='H')
    {
        strcpy(temp,str+=2); //copy word starting from second character to temp
        append[0]=first;
        strcat(temp,append);//copy the first char of word to end of temp
        temp[strlen(temp)+1]='\0';    // Null terminate temp
        strcat(temp,"HAY");        // append "HAY" to the string.
    }

    if(str[1]=='U')
    {
        strcpy(temp,str+=2); //copy word starting from second character to temp
        append[0]=first;
        strcat(temp,append);//copy the first char of word to end of temp
        temp[strlen(temp)+1]='\0';    // Null terminate temp
        strcat(temp,"UAY");        // append "HAY" to the string.
    }

    return temp;
}




char * last_rule(char *str)
{
    static char temp[80];
    memset(temp,'\0',80);

    strcpy(temp,++str);      //copy word starting from second character to temp
    temp[strlen(temp)]=*(--str); //copy the first char of word to end of temp
    temp[strlen(temp)+1]= '\0';   // Null terminate temp
    strcat(temp,"AY");        // append "way" to the string.
    return temp;
}

not looking at code improperly formatted. please edit your post to include code tags if you want help.

Here is a formatted version, I don't know why the last one didnt post this way.

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



char * vowel_rule(char *);
char * two_letter(char *);
char * last_rule(char *);
void rules();



int main()
{


rules();    //call function to read in sentence & print piglatin



return(0);
}


void rules()
{
char str[80];
char english[80];
int length,a,b,i;


i=0;
a=b=0;



printf("Please enter a phrase to be translated to piglatin: ");
fgets(str,79,stdin);    //store sentence in str


length = strlen(str);


printf("\nThis is your translated sentence: ");


while(a<=length)
{
english=str[a]; //copy str to english


if(english==' '||english=='\0') //check for whitespace
{
english='\0';
b=0;


if(english[0]=='A'||english[0]=='E'||english[0]=='I'||english[0]=='O'||english[0]=='U'||english[0]=='a'||english[0]=='e'||english[0]=='i'||english[0]=='o'||english[0]=='u')    //check for word beginning with vowel
{
printf(" %s",vowel_rule(english));


}


else if(english[0]=='T'&&english[1]=='H'||english[0]=='C'&&english[1]=='H'||english[0]=='S'&&english[1]=='H'||english[0]=='P'&&english[1]=='H'||english[0]=='W'&&english[1]=='H'||english[0]=='Q'&&english[1]=='U') //checks for first 2 letters
{
printf(" %s",two_letter(english));
}
else
{
printf(" %s",last_rule(english));
}
}
else
{
b++;
}


a++;
}


}


char * vowel_rule(char *str)
{
static char temp[80];
memset(temp,'\0',80);


strcpy(temp,str);   //copy word starting from second character to temp
temp[strlen(temp)+1]= '\0'; // Null terminate temp
strcat(temp,"AY"); // append "AY" to the string.


return temp;
}



char * two_letter(char *str)
{
static char temp[80];
memset(temp,'\0',80);
char first,append[]="H";


first=str[0];   //copy first char of str into first


if(str[1]=='H')
{
strcpy(temp,str+=2); //copy word starting from third character to temp
append[0]=first;
strcat(temp,append);//copy the first char of word to end of temp
temp[strlen(temp)+1]='\0'; // Null terminate temp
strcat(temp,"HAY"); // append "HAY" to the string.
}


if(str[1]=='U')
{
strcpy(temp,str+=2); //copy word starting from third character to temp
append[0]=first;
strcat(temp,append);//copy the first char of word to end of temp
temp[strlen(temp)+1]='\0'; // Null terminate temp
strcat(temp,"UAY"); // append "UAY" to the string.
}


return temp;
}



char * last_rule(char *str)
{
static char temp[80];
memset(temp,'\0',80);


strcpy(temp,++str); //copy word starting from second character to temp
temp[strlen(temp)]=*(--str); //copy the first char of word to end of temp
temp[strlen(temp)+1]= '\0'; // Null terminate temp
strcat(temp,"AY"); // append "AY" to the string.


return temp;
}

I am ahving a problem with the do/while loop. When the YesNo function returns a yes, main() repeats;however, it prints out what is stored in the string from before. How do you clear the string in order to run the program over and over. Also, when the piglatin result is printed, the last 3 letters are placed on the next line, why is this?

Here is the code thus far:

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


char * vowel_rule(char *);
char * two_letter(char *);
char * last_rule(char *);
void rules();
int YesNo();
void instructions();

int main()
{
    do
    {
        instructions();
        rules();    //call function to read in sentence & print piglatin

    }while(YesNo() != 0);

return(0);
}



void rules()
{
    char str[80];
    char english[80];
    int length,a,b,i;

    i=0;
    a=b=0;

    printf("Please enter a phrase to be translated to piglatin: ");
    fgets(str,79,stdin);    //store sentence in str

    length = strlen(str);

    printf("\nThis is your translated sentence: ");

    while(a<=length)
    {
        english[b]=str[a];  //copy str to english

        if(english[b]==' '||english[b]=='\0')   //check for whitespace
        {
            english[b]='\0';
            b=0;

            if(english[0]=='A'||english[0]=='E'||english[0]=='I'||english[0]=='O'||english[0]=='U'||english[0]=='a'||english[0]=='e'||english[0]=='i'||english[0]=='o'||english[0]=='u')    //check for word beginning with vowel
            { 
                printf(" %s",vowel_rule(english));

            }

            else if(english[0]=='T'&&english[1]=='H'||english[0]=='C'&&english[1]=='H'||english[0]=='S'&&english[1]=='H'||english[0]=='P'&&english[1]=='H'||english[0]=='W'&&english[1]=='H'||english[0]=='Q'&&english[1]=='U') //checks for first 2 letters
                {
                printf(" %s",two_letter(english));
                }
            else
            {
            printf(" %s",last_rule(english));   
            }
        }
        else
        {
            b++;
        }

        a++;
    }

}




void instructions()
{
    printf("This program will translate an english word or phrase up to 80 characters\n");
    printf(" into piglatin. The program will convert your phrase according to the\n");
    printf(" following rules:\n\n");
    printf("     Rule 1:  If the word begins with a vowel, 'AY' will be appneded to the\n");
    printf("                end.\n");
    printf("     Rule 2:  If the word begins with 'TH','SH','CH','WH','PH',or 'QU', the\n");
    printf("                first two letters are appended to the end of the word\n");
    printf("                followed by 'AY'\n\n");
    printf("     Rule 3:  If none of the rules apply, the first letter is appended to\n");
    printf("                the end of the word, followed by 'AY'.");
    printf("\n\nThe program will also check for case sensitivity. The output will match the \n");
    printf("   input, whether it is entered in lowercase or uppercase.\n\n\n\n");



}




char * vowel_rule(char *str)
{
    static char temp[80];
    memset(temp,'\0',80);

    strcpy(temp,str);   //copy word starting from second character to temp
    temp[strlen(temp)+1]= '\0'; // Null terminate temp
    strcat(temp,"AY"); // append "AY" to the string.

    return temp;
}




char * two_letter(char *str)
{
    static char temp[80];
    memset(temp,'\0',80);
    char first,append[]="H";

    first=str[0];   //copy first char of str into first

    if(str[1]=='H')
    {
        strcpy(temp,str+=2); //copy word starting from third character to temp
        append[0]=first;
        strcat(temp,append);//copy the first char of word to end of temp
        temp[strlen(temp)+1]='\0'; // Null terminate temp
        strcat(temp,"HAY"); // append "HAY" to the string.
    }

    if(str[1]=='U')
    {
    strcpy(temp,str+=2); //copy word starting from third character to temp
    append[0]=first;
    strcat(temp,append);//copy the first char of word to end of temp
    temp[strlen(temp)+1]='\0'; // Null terminate temp
    strcat(temp,"UAY"); // append "UAY" to the string.
    }

    return temp;
}




char * last_rule(char *str)
{
    static char temp[80];
    memset(temp,'\0',80);

    strcpy(temp,++str); //copy word starting from second character to temp
    temp[strlen(temp)]=*(--str); //copy the first char of word to end of temp
    temp[strlen(temp)+1]= '\0'; // Null terminate temp
    strcat(temp,"AY"); // append "AY" to the string.

    return temp;
}



int YesNo()
{
    char check;

    printf("\n\nWould you like to enter another sentence to be translated(Y/N)?:  ");
    scanf(" %c", &check);

    if(check=='Y'||check=='y')
    {
        return (1);
    }
    else
        return (0);

}//end of function

im betting the newline thing is the newline left in buffer by fgets(). Check the docs for fgets() and you will see that it leaves a newline in the buffer that often you want removed. You haven't removed it so thats why your word splits.
As for storing the words make a buffer in the rules func and instead of printing the words calling your different rule funcs in a printf, fill the buffer with the return result of the funcs something like strcpy(your_buff,last_rule(english));. Then print the buffer out instead.

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.