Chong's Avatar, Join Date: May 2011
Go4Expert Member
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 like this
won212's Avatar, Join Date: Mar 2012
Go4Expert Member
Quote:
p=tmp;
what does this do?

Quote:
*(p+strlen(word)-1)=consonant;//make p="bcd[consonant]

strcat(p, pig_latin);
}
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!
Chong's Avatar, Join Date: May 2011
Go4Expert Member
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[i]=*(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
Chong's Avatar, Join Date: May 2011
Go4Expert Member
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 like this
won212's Avatar, Join Date: Mar 2012
Go4Expert Member
Hi chong, thank you so much, I have a better understanding now! Kudos to u!
won212's Avatar, Join Date: Mar 2012
Go4Expert Member
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.
won212's Avatar, Join Date: Mar 2012
Go4Expert Member
Hi Chong, is okay, manage to solve the error!