944,135 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 3164
  • C RSS
Aug 7th, 2005
0

Need Help With Piglatin Program In C

Expand Post »
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;
}
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
cjones5499 is offline Offline
14 posts
since Aug 2005
Aug 7th, 2005
0

Re: Need Help With Piglatin Program In C

not looking at code improperly formatted. please edit your post to include code tags if you want help.
Reputation Points: 19
Solved Threads: 5
Junior Poster
Stoned_coder is offline Offline
164 posts
since Jul 2005
Aug 8th, 2005
0

Re: Need Help With Piglatin Program In C

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

}



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;
}
Reputation Points: 10
Solved Threads: 0
Newbie Poster
cjones5499 is offline Offline
14 posts
since Aug 2005
Aug 8th, 2005
0

Re: Need Help With Piglatin Program In C

Reputation Points: 10
Solved Threads: 5
Junior Poster in Training
zyruz is offline Offline
60 posts
since Jun 2005
Aug 8th, 2005
0

Re: Need Help With Piglatin Program In C

[code]
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:

  1. #include<stdio.h>
  2. #include<ctype.h>
  3. #include<string.h>
  4.  
  5.  
  6. char * vowel_rule(char *);
  7. char * two_letter(char *);
  8. char * last_rule(char *);
  9. void rules();
  10. int YesNo();
  11. void instructions();
  12.  
  13. int main()
  14. {
  15. do
  16. {
  17. instructions();
  18. rules(); //call function to read in sentence & print piglatin
  19.  
  20. }while(YesNo() != 0);
  21.  
  22. return(0);
  23. }
  24.  
  25.  
  26.  
  27. void rules()
  28. {
  29. char str[80];
  30. char english[80];
  31. int length,a,b,i;
  32.  
  33. i=0;
  34. a=b=0;
  35.  
  36. printf("Please enter a phrase to be translated to piglatin: ");
  37. fgets(str,79,stdin); //store sentence in str
  38.  
  39. length = strlen(str);
  40.  
  41. printf("\nThis is your translated sentence: ");
  42.  
  43. while(a<=length)
  44. {
  45. english[b]=str[a]; //copy str to english
  46.  
  47. if(english[b]==' '||english[b]=='\0') //check for whitespace
  48. {
  49. english[b]='\0';
  50. b=0;
  51.  
  52. 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
  53. {
  54. printf(" %s",vowel_rule(english));
  55.  
  56. }
  57.  
  58. 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
  59. {
  60. printf(" %s",two_letter(english));
  61. }
  62. else
  63. {
  64. printf(" %s",last_rule(english));
  65. }
  66. }
  67. else
  68. {
  69. b++;
  70. }
  71.  
  72. a++;
  73. }
  74.  
  75. }
  76.  
  77.  
  78.  
  79.  
  80. void instructions()
  81. {
  82. printf("This program will translate an english word or phrase up to 80 characters\n");
  83. printf(" into piglatin. The program will convert your phrase according to the\n");
  84. printf(" following rules:\n\n");
  85. printf(" Rule 1: If the word begins with a vowel, 'AY' will be appneded to the\n");
  86. printf(" end.\n");
  87. printf(" Rule 2: If the word begins with 'TH','SH','CH','WH','PH',or 'QU', the\n");
  88. printf(" first two letters are appended to the end of the word\n");
  89. printf(" followed by 'AY'\n\n");
  90. printf(" Rule 3: If none of the rules apply, the first letter is appended to\n");
  91. printf(" the end of the word, followed by 'AY'.");
  92. printf("\n\nThe program will also check for case sensitivity. The output will match the \n");
  93. printf(" input, whether it is entered in lowercase or uppercase.\n\n\n\n");
  94.  
  95.  
  96.  
  97. }
  98.  
  99.  
  100.  
  101.  
  102. char * vowel_rule(char *str)
  103. {
  104. static char temp[80];
  105. memset(temp,'\0',80);
  106.  
  107. strcpy(temp,str); //copy word starting from second character to temp
  108. temp[strlen(temp)+1]= '\0'; // Null terminate temp
  109. strcat(temp,"AY"); // append "AY" to the string.
  110.  
  111. return temp;
  112. }
  113.  
  114.  
  115.  
  116.  
  117. char * two_letter(char *str)
  118. {
  119. static char temp[80];
  120. memset(temp,'\0',80);
  121. char first,append[]="H";
  122.  
  123. first=str[0]; //copy first char of str into first
  124.  
  125. if(str[1]=='H')
  126. {
  127. strcpy(temp,str+=2); //copy word starting from third character to temp
  128. append[0]=first;
  129. strcat(temp,append);//copy the first char of word to end of temp
  130. temp[strlen(temp)+1]='\0'; // Null terminate temp
  131. strcat(temp,"HAY"); // append "HAY" to the string.
  132. }
  133.  
  134. if(str[1]=='U')
  135. {
  136. strcpy(temp,str+=2); //copy word starting from third character to temp
  137. append[0]=first;
  138. strcat(temp,append);//copy the first char of word to end of temp
  139. temp[strlen(temp)+1]='\0'; // Null terminate temp
  140. strcat(temp,"UAY"); // append "UAY" to the string.
  141. }
  142.  
  143. return temp;
  144. }
  145.  
  146.  
  147.  
  148.  
  149. char * last_rule(char *str)
  150. {
  151. static char temp[80];
  152. memset(temp,'\0',80);
  153.  
  154. strcpy(temp,++str); //copy word starting from second character to temp
  155. temp[strlen(temp)]=*(--str); //copy the first char of word to end of temp
  156. temp[strlen(temp)+1]= '\0'; // Null terminate temp
  157. strcat(temp,"AY"); // append "AY" to the string.
  158.  
  159. return temp;
  160. }
  161.  
  162.  
  163.  
  164. int YesNo()
  165. {
  166. char check;
  167.  
  168. printf("\n\nWould you like to enter another sentence to be translated(Y/N)?: ");
  169. scanf(" %c", &check);
  170.  
  171. if(check=='Y'||check=='y')
  172. {
  173. return (1);
  174. }
  175. else
  176. return (0);
  177.  
  178. }//end of function
Reputation Points: 10
Solved Threads: 0
Newbie Poster
cjones5499 is offline Offline
14 posts
since Aug 2005
Aug 8th, 2005
0

Re: Need Help With Piglatin Program In C

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.
Reputation Points: 19
Solved Threads: 5
Junior Poster
Stoned_coder is offline Offline
164 posts
since Jul 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Data Structure Problem
Next Thread in C Forum Timeline: problem again





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC