Need help in pig latin!

Discussion in 'C++' started by won212, Mar 3, 2012.

  1. won212

    won212 New Member

    Joined:
    Mar 3, 2012
    Messages:
    17
    Likes Received:
    2
    Trophy Points:
    0
    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);	
    }
    
    
     
  2. Chong

    Chong New Member

    Joined:
    May 15, 2011
    Messages:
    29
    Likes Received:
    7
    Trophy Points:
    0
    Hi W
    Can you tell me what the output is supposed to be?
    Best regards
    Chong
     
  3. won212

    won212 New Member

    Joined:
    Mar 3, 2012
    Messages:
    17
    Likes Received:
    2
    Trophy Points:
    0
    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.
     
  4. Chong

    Chong New Member

    Joined:
    May 15, 2011
    Messages:
    29
    Likes Received:
    7
    Trophy Points:
    0
    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 likes this.
  5. won212

    won212 New Member

    Joined:
    Mar 3, 2012
    Messages:
    17
    Likes Received:
    2
    Trophy Points:
    0
    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
     
    shabbir and coderzone like this.
  6. Chong

    Chong New Member

    Joined:
    May 15, 2011
    Messages:
    29
    Likes Received:
    7
    Trophy Points:
    0
    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
     
  7. won212

    won212 New Member

    Joined:
    Mar 3, 2012
    Messages:
    17
    Likes Received:
    2
    Trophy Points:
    0
    Hi thanks alot for your help but I still quite a few bugs :(. Will try it out.
     
  8. won212

    won212 New Member

    Joined:
    Mar 3, 2012
    Messages:
    17
    Likes Received:
    2
    Trophy Points:
    0
    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?
     
  9. Chong

    Chong New Member

    Joined:
    May 15, 2011
    Messages:
    29
    Likes Received:
    7
    Trophy Points:
    0
    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 and won212 like this.
  10. won212

    won212 New Member

    Joined:
    Mar 3, 2012
    Messages:
    17
    Likes Received:
    2
    Trophy Points:
    0
    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
     
  11. Chong

    Chong New Member

    Joined:
    May 15, 2011
    Messages:
    29
    Likes Received:
    7
    Trophy Points:
    0
    Hi won
    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); 
    }
    
     
    
     
    won212 likes this.
  12. won212

    won212 New Member

    Joined:
    Mar 3, 2012
    Messages:
    17
    Likes Received:
    2
    Trophy Points:
    0
    what does this do?

    I do not really understand this code.
    why do we have to find the string length and sum it with p and what does the -1 do?
    [/CODE]

    Thank you for your useful post!
     
  13. Chong

    Chong New Member

    Joined:
    May 15, 2011
    Messages:
    29
    Likes Received:
    7
    Trophy Points:
    0
    Hi won

    Suppose that the input is "happy". Then p[] = word[]="happy" and consonant is 'h' . strcpy(p,p+1) makes p[]="appy" and we let p[4]=p[strlen(word)-1]=consonant. Then we have p[]="appyh". Note that p=*(p+i).

    Note that the value of p in the function changes from time to time. We need to know the value of p when the function is called so tmp points to the value of p intially. For example.
    char p[]="happy";
    char *tmp=p;
    printf("%s\n",p);//This prints "happy"
    p=p+1;
    printf("%s\n",p);//This prints "appy"
    p=tmp;
    printf("%s\n",p);//This prints "happy"

    Best regards
    Chong
     
  14. Chong

    Chong New Member

    Joined:
    May 15, 2011
    Messages:
    29
    Likes Received:
    7
    Trophy Points:
    0
    The last example should be
    char name[]="happy";
    char *p=name, *tmp=p;
    printf("%s\n",p);//prints "happy"
    p=p+1;
    printf("%s\n",p);//prints "appy"
    p=p+1;
    printf("%s\n",p);//prints "ppy"
    p=tmp;
    printf("%s\n",p);//prints "happy"

    Note that name[] is a constant array so name's value can not change.
     
    won212 likes this.
  15. won212

    won212 New Member

    Joined:
    Mar 3, 2012
    Messages:
    17
    Likes Received:
    2
    Trophy Points:
    0
    Hi chong, thank you so much, I have a better understanding now! Kudos to u!
     
  16. won212

    won212 New Member

    Joined:
    Mar 3, 2012
    Messages:
    17
    Likes Received:
    2
    Trophy Points:
    0
    Hi Chong,

    Do you know can I compare 2D array same column by same row? How can I compare the same row with same column to derive the maximum value? For examples, column 1 will be comparing with row 1, column 2 will be comparing with row 2, column 3 with row 3 and so on...

    I did some c++ coding but it didn't works.

    I guess I do not know yet to use what method for comparing same column and same row together, what code can ensure this?

    By the way, my error is I have a 5x5 array, my first to fourth looping manage to get the correct maximum value. How ever, for the last, 5th position which is comparing array[4][0] with [0][4], I got the correct maximum value but did not manage to get the correct position [][] of the maximum value.

    Thanks.
     
  17. won212

    won212 New Member

    Joined:
    Mar 3, 2012
    Messages:
    17
    Likes Received:
    2
    Trophy Points:
    0
    Hi Chong, is okay, manage to solve the error! :happy:
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice