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