| | |
Please help with program - STUCK
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Aug 2005
Posts: 14
Reputation:
Solved Threads: 0
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:
Here is the code thus far:
C Syntax (Toggle Plain Text)
#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
•
•
•
•
Originally Posted by cyber_aziz
you can clear a string like this
char ch;
char ch = {' '}
•
•
•
•
Originally Posted by cyber_aziz
you can clear a string like this
char ch;
ch[] = {' '};
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
•
•
Join Date: Jul 2005
Posts: 244
Reputation:
Solved Threads: 5
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.
Or, you could clear the str array after you increment a.
C Syntax (Toggle Plain Text)
a++; str[--a] = ' ';
You need to read the '\n' from the input buffer -- scanf leaves it there.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
![]() |
Similar Threads
- Help on a poker program (C++)
Other Threads in the C Forum
- Previous Thread: pls i need help
- Next Thread: graphices header file
| Thread Tools | Search this Thread |
#include * ansi append array arrays asterisks bash binarysearch centimeter changingto char character convert copyimagefile cprogramme creafecopyofanytypeoffileinc database dynamic execv feet fgets file floatingpointvalidation fork framework function getlogicaldrivestrin givemetehcodez grade gtkwinlinux hacking histogram ide inches include incrementoperators infiniteloop initialization input interest intmain() iso kernel keyboard kilometer license linked linkedlist linux list lists locate looping lowest matrix meter microsoft number oddnumber opendocumentformat openwebfoundation overwrite owf pdf pointer posix power probleminc process program programming radix recursion recv recvblocked research reversing segmentationfault sequential single socket socketprograming socketprogramming standard strchr string suggestions systemcall test testing threads turboc unix urboc user variable wab whythiscodecausesegmentationfault windowsapi






