here is the code
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define SIZE 120
int RuleApplied(char letters[]);
void rule1(char word[],char newword[]);
void rule2(char word[], char newword[],int length);
void rule3(char word[], char newword[]);
void DisplayIntruc();
int findnonblank (char sentencein[],int i);
int findblank (char sentencein[], int i);
void Firstletter(char letters[], char word[]);
void Secondletter(char letters[], char word[]);
void getWord(char sentence[], char word[],int b, int e);
int main()
{
int selection;
int length;
char trash[1]="\0";
char sentencein[SIZE];
char sentenceout[SIZE*3];
char word[20]="";
int position=0;
int nonBlank;
int blank;
int rule;
char newword[20];
char letters[2];
FILE *outp;
outp=fopen("output.txt","w");
printf("Welcome to PIGLATIN\n");
printf("enter seletion 1 to quit, any other number to continue\n");
scanf("%d",&selection);
while (selection != 1)
{
DisplayIntruc();
scanf("%c",&trash);
printf("Enter a sentence with less than 80 blah:\n");
gets(sentencein);
fprintf(outp, "\nYour sentence: %s\n",sentencein);
printf("\nYour sentence:%s\n",sentencein);
strcpy(sentenceout,"");
length =strlen(sentencein);
printf("length:%d\n", length);
position=0;
while(sentencein[position]!='\0')
{
nonBlank = findnonblank(sentencein, position);
blank= findblank(sentencein,nonBlank);
strcpy(word,"");
strcpy(newword,"");
getWord(sentencein,word,nonBlank,blank);
printf("\nWORD:%s",word);
Firstletter (letters,word);
Secondletter (letters,word);
rule = RuleApplied(letters);
if(rule==1)
{
rule1(word,newword);
}
else if(rule==2)
{
rule2(word,newword);
}
else
{
rule3(word,newword);
}
printf("\nNEW WORD:%s",newword);
strcat(sentenceout,newword);
position = blank;
}
printf("\n\nSEntence out:%s\n",sentenceout);
fprintf(outp, "\n\nSentence out:%s\n",sentenceout);
printf("\nDo you want to continue? Type 1 if you want to exit.\n");
scanf("%d",&selection);
fclose(outp);
}
return 0;
}
void DisplayIntruc ()
{
printf("good choice!!\n");
printf("You will enter a sentence of 80 chac or less.\n");
printf("Depending on your words, you will be given a translation\n");
printf("Lets get started\n");
}
void getWord(char sentence[],char word[], int b, int e)
{
strncat(word,&sentence[b],(e-B)/>);
}
int findnonblank(char sentencein[], int i)
{
while(sentencein[i]==' ')
{
i++;
}
return i;
}
int findblank (char sentencein[],int i)
{
while (sentencein[i]!=' ')
{
if(sentencein[i]=='\0')
{
return i;
}
i++;
}
return i;
}
void Firstletter(char letters[],char word[])
{
letters[0]= toupper(word[0]);
}
void Secondletter (char letters[],char word[])
{
letters[1]= toupper(word[1]);
}
int RuleApplied(char letters[])
{
if(strncmp(letters,"TH",2)==0)
return 1;
else if (strncmp(letters,"WH",2)==0)
return 1;
else if (strncmp(letters,"SH",2)==0)
return 1;
else if (strncmp(letters,"QU",2)==0)
return 1;
else if(letters[0] =='A'||letters[0]=='E'||letters[0]=='I'|| letters[0] =='O'|| letters[0] =='U')
{
return 2;
}
else
{
return 3;
}
}
void rule1(char word[],char newword[])
{
strcpy(newword,&word[2]);
strncat(newword,word,2);
strncat(newword,"AY");
}
void rule2(char word[], char newword[], int length)
{
strcpy(newword,word);
strcat(newword,"AY");
}
void rule3 (char word[], char newword[])
{
strcpy(newword,&word[1]);
strncat(newword,word,1);
strcat(newword,"AY");
}
This post has been edited by no2pencil: 03 April 2012 - 06:19 PM
Reason for edit:: Added code tags

New Topic/Question
Reply



MultiQuote





|