The following program below works on my computer. You need to get used to pointers because they are useful and used very often. Ask me if any does not make sense to you.
Best regards
Chong
Code:
#include <stdio.h>
//#include <iostream>
#include <string>
#define SIZE 90
void convertPigLatin(char *word,char *p);
main()
{
char word[SIZE];
char piglatin[SIZE];
printf("Input: ");
//cout << "Input:";
scanf("%s", word);
//cin >> word;
convertPigLatin(word,piglatin);
printf("Pig Latin: %s\n", piglatin);
//cout <<"Pig Latin: " << piglatin << '\n';
}
void convertPigLatin(char *word, char *p)
{
char *pig_latin="ay";
char *tmp=p;
strcpy(p,word);//We need this.
//Check if the input has consonants only.
while (strchr("aeiouAEIOU",*p)==NULL){
p++;
if (*p==NULL){//check if *p is end of string
//the input has consonants only
strcat(tmp,pig_latin);
return;
}//if
}//while
p=tmp;
//Continue the loop if p[0] is consonant
while (strchr("aeiouAEIOU",*p) == NULL){
char consonant=*p;
strcpy(p,p+1); //if p="abcd", then make p="bcd"
*(p+strlen(word)-1)=consonant;//make p="bcd[consonant]"
}//while
strcat(p, pig_latin);
}
