Hi
I wrote this program that converts a sentence into pig latin. I wrote this a while ago, now just wanna see if i can improve it and see what other people think about it
It follows these rules:-
If the word begins with a vowel -way is appended at the end.
An become Anway
If the word has no vowel -way is appended at the end.
By becomes Byway
Otherwise all the letters following the first vowel in the word are moved to the front and -ay is added to the end.
Pond becomes Ondpay.
here is the code, tell me what ya think and how i can improve it:-
Code:
/* This program takes in a line of text, and converts into pig latin, and the rules*/
/*of pig latin are explined in the program details*/
/*programmer = Kashif Khan. 139-06-1856*/
/*TTH 4:00-5:30*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
/*function prototypes*/
void translateline(char [], char []);
int extractintoew(char [], char [],int);
void convertintopig(char [], char [] );
int wherevowel(char []);
int copyintopl(char [], char [],int);
int main()
{
char el[100]={'\0'},pl[200]={'\0'};
printf("Enter a line(# to end): ");
gets(el);
while(el[0]!='#')
{
translateline(el,pl);
printf("Pig Latin translation: ");
puts(pl);
printf("\nPlease enter a line (# to end): ");
fflush(stdin);
gets(el);
}
printf("Good Bye, Oodgay Ebyay\n");
system("PAUSE");
return 0;
}
/*based on pseudo code shown in instruction*/
void translateline(char el[], char pl[])
{
char ew[17] = {'\0'},pw[17] = {'\0'};
int ep = 0, pp = 0, k = 0,r;
while (el[ep] != '\0')
{
if (isalpha(el[ep]))
{
k = extractintoew(el,ew,ep);
ep += k;
convertintopig(ew,pw);
r = copyintopl(pw,pl,pp);
pp += r;
}
else
{
if (isspace(el[ep]))
pl[pp++] = el[ep];
else
{
pl[pp] = el[ep];
pp++;
}
ep++;
}
}
pl[pp] = '\0';
}
/*extracts word from el that starts from current position*/
/*and store in ew*/
int extractintoew(char el[],char ew[],int ep)
{
int i = 0;
while (isalpha(el[ep]))
ew[i++] = el[ep++];
ew[i] = '\0';
return i;
}
/*translate English word ew to Pig Latin and store in pw*/
void convertintopig(char ew[], char pw[])
{
int y, m, cap = 0;
/* Check for a capital letter */
if (ew[0] == toupper(ew[0]))
{
ew[0] = (char)tolower(ew[0]);
cap = 1;
}
y = wherevowel(ew);
if (y == 0 || y == -1)
{
strcat(ew,"way");
strcpy(pw,ew);
}
else
{
m=strlen(ew);//calculate the length of the string;
memmove(pw, ew + y, m); /* Copy the latter half to pw */
strncat(pw, ew, y); /* Copy the former half to pw */
strcat(pw, "ay"); /* End the word */
}
/* Deal with the capital letter */
if (cap == 1)
pw[0] = (char)toupper(pw[0]);
}
/*determines the position of the first vowel*/
int wherevowel(char ew[])
{
int j;
char *vowel = "aeiou";
if(ew[0]=='a'||ew[0]=='e'||ew[0]=='i'||ew[0]=='o'
||ew[0]=='u'||ew[0]=='A'||ew[0]=='E'||ew[0]=='I'
||ew[0]=='O'||ew[0]=='U')
{
j = 0;
}
else
{
char *p = strpbrk(ew,vowel);
j = (p == 0) ? -1 : p - ew;
}
return j;
}
/*copies word from pw into pl*/
int copyintopl(char pw[], char pl[], int pp)
{
int a = 0;
int b;
b = strlen(pw);
while (a < b)
pl[pp++] = pw[a++];
return b;
}