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

you can clear a string like this

char ch;
char ch = {' '}

Here since ch is a single character it cannot be a string.

you can clear a string like this

char ch;
ch[] = {' '};

And this is even worse. You cannot assign to arrays. You need a subscript. And it's still a single character.

If neither of those will work, what are my options this program to clear the array for the second input?

You could do a simple loop for the length of str that writes blankspace in for each entry in the array.
Or, you could clear the str array after you increment a.

a++;
str[--a] = ' ';

I tried using str and also tried a for loop setting blank spaces, neither worked, when the program is ran the second loop run skips the user input and goes to YesNo again. Also, can anyone tell me why the last 3 chars printed go to the next line?

Why not use memset to clear the str, then? Just write in blanks or whatever for your 80 character entries, as you've done in last_rule?

I have tried to use memset and the for loop to allow the program to loop, but after the first run it skips the input and displays 'AY' and prompts the YesNo function. Can anyone tell me why this is, I cannot figure it out.

Thanx for the help on the '\n' - I took care of that problem

Also - I was just told we now need to check the phrase for upper and lower case in a function for each rule. Each letter must stay the same as the input - where is the best place in my program to put the call to a this new function? Is 'isupper', 'toupper' the best way to achieve this? I have not used these functions before.

isupper and islower take a single character entry and return a 1 if true, 0 if not.
However, you'll probably get a bit of a sadistic teacher who will throw numbers into the input, so you'll also want to make sure that you check both isupper and islower, or use the combined function isalpha first.

How do you say 1033843 in pig latin, anyway?

The best place to call the function (I assume you mean that if the first letter is upper-case, everything must be upper-case, even if entered in lower-case) would be directly after you read in the input, checking the first alphabet entry and calling isupper or islower appropriately. Those functions return the integer value of the input character (it's an int input, really, but c performs the char-to-int conversion implicitly) converted to the upper or lower case, as needed. Otherwise, it returns the value of the input integer.

This does mean that your vowel rule has to comply, you know.

I wrote this function for the rule functions to share. It reads in the string temp and check for any capital letters. If there are any, the whole word is capitalized, otherwise it is all lowercase. When I run it, i used a printf in the function to check value after the for loop - it is correct and the value of 'u' is correct. The if statement using 'u' doesnt work right - it only goes to the first if, never the second. No matter what is put in, all uppercase are returned -please help. I will post the whole program, the function is at the end, please try running it.

#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();
char * string_case(char *);

int main()
{
	instructions();

	do
	{
		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]!='\n')
	{
		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++;
		}
	}
	else
	{
		english[b]='\0';
	}
		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.

	string_case(temp);
	
	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



char * string_case(char *temp)
{
	int a=0,i,u=0;
	
	for(a=0;a<80;a++)
	{
		if(isupper(temp[a]))
			u=1;
	}

	printf("%d %s",u,temp);
	if(u=1)
	{
		for(i=0;i<80;i++){
		temp[i]=toupper(temp[i]);
		}printf("%s",temp);
		return(temp);

	}else if(u=0)
	{
		for(i=0;i<80;i++){
		temp[i]=tolower(temp[i]);
		}printf("%s",temp);
		return(temp);
	}
}

Disregard the last post, I am an idiot. I fixed the problem by changing to if(u!=0) and it works fine. The only problem I still have is the YesNo function. The program only works once and bypasses user input on the second,third... runs. Any ideas?

Disregard the last post, I am an idiot. I fixed the problem by changing to if(u!=0) and it works fine. The only problem I still have is the YesNo function. The program only works once and bypasses user input on the second,third... runs. Any ideas?

Already mentioned...

You need to read the '\n' from the input buffer -- scanf leaves it there.

int YesNo()
{
	char check;

	printf("\n\nWould you like to enter another sentence to be translated(Y/N)?:  ");
	scanf(" %c", &check);
	/* newline left in input buffer to screw up stuff later on */
	if(check=='Y'||check=='y')
		return (1);
	else
		return (0);

}//end of function

Ok, thanks for all the help thus far, I am almost done if I can get the YesNo loop to execute properly. I understan what you mean in that the newline is stored from the user input of YesNo; however, I can not figure out how to correct the problem. Here is the program I have up to date:

#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();
char * string_case(char *);
char * PigLatin(char *);

int main()
{
	instructions();

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

}



void rules()
{
	do{

	char str[80];
	char english[80];
	static char piglatin[500];
	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]!='\n')
	{
		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
			{ 
				strcat(piglatin,vowel_rule(english));

			}
			
			else if( (((english[0] == 'T' || english[0] == 't') || (english[0] == 'S' || english[0] == 's') || (english[0] == 'C' || english[0] == 'c') || (english[0] == 'W' || english[0] == 'w') || (english[0] == 'P' || english[0] == 'p')) && (english[1] == 'H' || english[1] == 'h')))
			{
				strcat(piglatin,two_letter(english));
			}

			else
			{
			strcat(piglatin,last_rule(english));	
			}
		}
		else
		{
			b++;
		}
	}
	else
	{
		english[b]='\0';
	}
		a++;


	}

	printf("%s",string_case(piglatin));
	
	}while(YesNo() != 0);

}




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.

	string_case(temp);
	
	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'||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.
		string_case(temp);
	}
	
	if(str[1]=='U'||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.
	string_case(temp);
	}

	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.
	string_case(temp);
	
	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



char * string_case(char *piglatin)
{
	int a=0,i,u=0;
	
	for(a=0;a<500;a++)
	{
		if(isupper(piglatin[a]))
			u=1;
	}

	if(u!=0)
	{
		for(i=0;i<80;i++){
		piglatin[i]=toupper(piglatin[i]);
		}
		return(piglatin);

	}else
	{
		for(i=0;i<500;i++){
		piglatin[i]=tolower(piglatin[i]);
		}
		return(piglatin);
	}
}

I would appreciate any help in further explanation

Ok, I am ready to shoot myself. I have been at this for over 2 hours, and I cant get the second run to work. Can someone please show me in a little more detail how to fix this problem, I dont have much time left. I have tried solutions I've found in other posts to clear the newline char from str and I still have no luck. Please run program and HELP if you can pinpoint the problem, I am not very experienced with C.

Here is the code:

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


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

int main()
{

	instructions();

	do
	{
	char str[80];
	int length;
	
	printf("Please enter a phrase to be translated to piglatin: ");
	fgets(str,79,stdin);

	length = strlen(str);

	rules(str,length);
	}while(YesNo() != 0);

	return(0);
}



void rules(char*str,int length)
{
	char english[80];
	static char piglatin[500];
	int a,b,i;
	i=0;
	a=b=0;
	

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

	if(english[b]!='\n')
	{
		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
			{ 
				strcat(piglatin,vowel_rule(english));

			}
			
			else if( (((english[0] == 'T' || english[0] == 't') || (english[0] == 'S' || english[0] == 's') || (english[0] == 'C' || english[0] == 'c') || (english[0] == 'W' || english[0] == 'w') || (english[0] == 'P' || english[0] == 'p')) && (english[1] == 'H' || english[1] == 'h')))
			{
				strcat(piglatin,two_letter(english));
			}

			else
			{
			strcat(piglatin,last_rule(english));	
			}
		}
		else
		{
			b++;
		}
	}
	else
	{
		english[b]='\0';
	}
		a++;


	}

	printf("This is your translated sentence: %s",string_case(piglatin));
	

}




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'||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'||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



char * string_case(char *piglatin)
{
	int a=0,i,u=0;
	
	for(a=0;a<500;a++)
	{
		if(isupper(piglatin[a]))
			u=1;
	}
	if(u!=0)
	{
		for(i=0;i<80;i++){
		piglatin[i]=toupper(piglatin[i]);
		}
		return(piglatin);

	}else
	{
		for(i=0;i<500;i++){
		piglatin[i]=tolower(piglatin[i]);
		}
		return(piglatin);
	}
}

Thank you for the time

One fix.

int YesNo()
{
	char check;

	printf("\n\nWould you like to enter another sentence to be translated(Y/N)?:  ");
	scanf("%c",&check);
	getchar();
	if(check=='Y'||check=='y')
		return(1);
	else
		return(0);

}//end of function

Another one, I think -- I only took a quick check.

void rules(char*str,int length)
{
	char english[80];
	static char piglatin[500];
	int a,b,i;
	i=0;
	a=b=0;
    piglatin[0] = '\0';

I can't thank you enough for your help. I finally got everything working, hopefully no more stipulations come from the instructor.

You've got a number of odd things in your code to think about, too.

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;
}

For example, strcpy will null terminate temp. To the next line you call strlen (which reads the number of characters up to the null terminator) to find the postition of an already existing null so that you can null terminate it.

And strcat null terminates as well. You can't use strlen to find out where to null terminate.

ok fine just proves how much of a beginner i am

I'm not trying to get down on you; but it would be quite helpful if you'd at least run some test code by a compiler before offering syntax errors as a solution.

This question has already been answered. Start a new discussion instead.