Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   Need help in pig latin! (http://www.go4expert.com/forums/help-pig-latin-t27944/)

won212 4Mar2012 00:42

Need help in pig latin!
 
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 4Mar2012 12:17

Re: Need help in pig latin!
 
Hi W
Can you tell me what the output is supposed to be?
Best regards
Chong

won212 4Mar2012 12:27

Re: Need help in pig latin!
 
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 4Mar2012 15:42

Re: Need help in pig latin!
 
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 4Mar2012 17:27

Re: Need help in pig latin!
 
Quote:

Originally Posted by Chong (Post 93053)
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

Chong 4Mar2012 18:36

Re: Need help in pig latin!
 
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 4Mar2012 19:08

Re: Need help in pig latin!
 
Quote:

Originally Posted by Chong (Post 93060)
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 4Mar2012 22:30

Re: Need help in pig latin!
 
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 4Mar2012 23:23

Re: Need help in pig latin!
 
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

won212 4Mar2012 23:29

Re: Need help in pig latin!
 
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 :D


All times are GMT +5.5. The time now is 05:34.