Need help in pig latin!

won212's Avatar, Join Date: Mar 2012
Go4Expert Member
Hi, I was just playing around with Pig Latin & I came across this codes somewhere. I was trying to debug it, there was no error but I got weird error like this.
Input: happy
Pig Latin: 汤汤汤汤汤汤汤汤汤汤汤汤汤汤汤汤汤汤汤汤汤 appyhay

Any c++ expert know how can i fix it? Thanks.

Code:
#include <stdio.h>
#include <string.h>
#define SIZE 90
void convertPigLatin(char *word, char *piglatin);

main()
{
        char word[SIZE];
	char piglatin[SIZE];
	printf("Input: ");
	scanf("%s", word);
	convertPigLatin(word, piglatin);
	printf("Pig Latin: %s\n", piglatin);
}

void convertPigLatin(char *word, char *piglatin)
{
	char *pig_latin="ay";
	char *after, *before;
	while (strchr("aeiouAEIOU",*word) == NULL)
	{
		char consonant=*word;
		piglatin=word, before=word+1;
		while (*before)
			*piglatin++=*before++;
		*piglatin=consonant;		
	}
	strcat(piglatin, pig_latin);	
}
Chong's Avatar, Join Date: May 2011
Go4Expert Member
Hi W
Can you tell me what the output is supposed to be?
Best regards
Chong
won212's Avatar, Join Date: Mar 2012
Go4Expert Member
when user types "aa" it will print "aaay".
when user types "thay" it will print "aythay"

It will loop until a letter of vowel is found and letter which is not vowel will move back.

Every word will + ay at the end.

Do you have a even more simple solutions for this?

I dont quite get this code actually.
Chong's Avatar, Join Date: May 2011
Go4Expert Member
Hi
Copy and paste the following program to see if it does what you want. Your original sample program does not do that job!
Best regards
Chong
Code:
//#include <stdio.h>
#include <iostream.h>
#include <string.h>

#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 *after, *before;
	char *tmp=p;
	char tmp_word[SIZE];

	strcpy(tmp_word,word);
	strcpy(p,word);
	//*(p+strlen(word))=NULL;

    while (strchr("aeiouAEIOU",*tmp_word) == NULL)

    {

        char consonant=*tmp_word;

		before=tmp_word+1;

        while (*before)

            *(p++)=*(before++);

        *p=consonant;
        p=tmp;
		strcpy(tmp_word,p);
    }

    strcat(p, pig_latin);    

}
won212 like this
won212's Avatar, Join Date: Mar 2012
Go4Expert Member
Quote:
Originally Posted by Chong View Post
Hi
Copy and paste the following program to see if it does what you want. Your original sample program does not do that job!
Best regards
Chong
Code:
//#include <stdio.h>
#include <iostream.h>
#include <string.h>
 
#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 *after, *before;
    char *tmp=p;
    char tmp_word[SIZE];
 
    strcpy(tmp_word,word);
    strcpy(p,word);
    //*(p+strlen(word))=NULL;
 
    while (strchr("aeiouAEIOU",*tmp_word) == NULL)
 
    {
 
        char consonant=*tmp_word;
 
        before=tmp_word+1;
 
        while (*before)
 
            *(p++)=*(before++);
 
        *p=consonant;
        p=tmp;
        strcpy(tmp_word,p);
    }
 
    strcat(p, pig_latin);    
 
}
Hi thank u so much!
I have tried to debug your code but i got the error C1083: Cannot open include file: 'iostream.h': No such file or directory and IntelliSense:cannot openn source file "iostream.h", cout is undefined, cin is undefined.

Used Mircosoft Visual C 2010 Express to debug it
coderzone, shabbir likes this
Chong's Avatar, Join Date: May 2011
Go4Expert Member
Hi Won
instread of <iostream.h>, use <iostream>. My sample program has a bug - if the input has no bowel, it will go into a loop and never get out. By the way, if the input is 'hounded', what is the output supposed to be?
Best regards
Chong
won212's Avatar, Join Date: Mar 2012
Go4Expert Member
Quote:
Originally Posted by Chong View Post
Hi Won
instread of <iostream.h>, use <iostream>. My sample program has a bug - if the input has no bowel, it will go into a loop and never get out. By the way, if the input is 'hounded', what is the output supposed to be?
Best regards
Chong
Hi thanks alot for your help but I still quite a few bugs . Will try it out.
won212's Avatar, Join Date: Mar 2012
Go4Expert Member
Hi Chong, tried your code, still has some error. Would you mind to help me comment on the steps u provided? I am just a beginner for c++ so pardon me, do you have shorter or easier steps?
Chong's Avatar, Join Date: May 2011
Go4Expert Member
Hi won212
I can help you. But I have one questioin to be answered by you first. When the input is "happier", what shoud be the output? which of 'appierhay' or 'aiehppray' should be the output? I have written two programs to cover both of them. First you have to tell me what the output should look like. Then I will give you the right program and then we will go through line by line with my comments. If the output should be 'aiehppray', the program is dead easy and you will laugh at how you have missed it. If the output should be 'appierhay', it is just a bit complicated. It is my bed time unfortuantely now. I will read my reply tomorrow morning.
Best regards
P.S.
I have been told that,if you use VS2010, <***.h> is no longer valid (i.e. remove '.h' so <iostream> for <iostream.h>.
Chong
shabbir, won212 likes this
won212's Avatar, Join Date: Mar 2012
Go4Expert Member
Hi Chong, your help is greatly appreciated! It should be "appierhay." I would like to make all the consonant before a the appearance of vowel towards the back. For examples, brown, it will print out ownbray. cout & sin method are abit tough for me, still a new beginner in c++, currently have learnt, printf method & a little of pointer. pardon me