NEED HELP IN PIG LATIN!

Mar 3, 2012 at 6:42pm
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#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);	
}
Mar 3, 2012 at 11:01pm
In line 23 you set piglatin to the address word holds, meaning that the memory pointed to by piglatin is never modified. It just happens that word follows piglatin on the stack so when you print out the junk contained by piglatin which doesn't contain a nul character to signal the end of a c-string, it keeps going until it reaches the nul character terminating word.
Mar 4, 2012 at 5:07am
So in line 13 should print word not piglatin?

printf("Pig Latin: %s\n", word);
Last edited on Mar 4, 2012 at 5:22am
Topic archived. No new replies allowed.