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); }
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.
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
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 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?
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
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
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); }
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!
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
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.
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.