i cant see the problem :(

This is a discussion on i cant see the problem :( within the C Programming forums, part of the General Programming Boards category; im not sure whats wrong with my code, but mabey someone can help me. I just started programming like 2 ...

i cant see the problem :(

im not sure whats wrong with my code, but mabey someone can help me. I just started programming like 2 and a half weeks ago, so if its that obvious, just bear with me Anyways, i know that the problem is in the function, and i dont know where it is. Oh, and if this is the second post about this question, thats my fault, but i dont think the first one worked for some reason. The point of the program is to translate an inputed sentance into pig latin, by taking the first letter of each word and placing it at the end of the word, followed by "ay"

while(!flag)
{
word[i]=word[i+1];
//shifts every character one over to the left

if (word[i]=='\0')
flag++;
i++;
}

strcat(word,tptr); //adds the first letter to the end

strcat(word,"ay");
}

strcat(word,tptr); this is where the problem is happening i think... My teacher told me something about using strlen to complete the function, but i just dont understand how, or where!! please help, ASAP, the program is due in like 3 days...

you initialize tptr to the address of a character, not a valid NULL-terminated string. For strcat to work, both arguments must be NULL-terminated. strcat expects to find a NULL byte at the end of the string, so it will continue on into other parts of memory until it runs into one. A possible solution would be to declare tptr as a two byte array, set the first character to word[0], and the second to '\0', making it a valid string, and satisfying the strcat function

void printLatinWord(char *myWord) {
char tempChar='\0';
int wordLen=0;
char tempWord[MAX_SIZE] = { '\0' };
char *myWordPtr = tempWord;
// set tempChar to the first letter of the word, cycle each letter down by one, and
// append the letter to the end of the word.
tempChar = myWord[0];
wordLen = strlen(myWord);
int i;
for (i = 0; i < (wordLen - 1); i++) /* we go to wordLen - 1 so we can overwrite
the last letter with the first letter, and
then append 'a' 'y' '\0' */
myWordPtr[ i] = myWord[i+1];
myWordPtr[ i] = tempChar; // append the first letter
myWordPtr[++i] = '\0'; // strcat is expecting an ASCIIZ string
strcat(myWordPtr,"ay"); // should add the \0
printf("%s",myWordPtr);
}

So you can see strlen() is used to return the correct index* of the null terminator; You overwrite the null terminator with the first letter from the word, and then put the null terminator in index+1 (or "i" in the case of my code). Using strcat to add the "ay" takes care of the null terminator automatically.

*because we used a for loop that terminated at strlen(myWord) - 1, the for loop's counter ends up equal to strlen(myWord), and there's no need for a second call to strlen(), although you can add it in if you find it makes your code easier to understand.