Thread: i cant see the problem :(

  1. #1
    Registered User
    Join Date
    Jan 2002

    Unhappy 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"

    This is my code so far :

    /Alicia Hogue
    //Chapitre 8
    //Exercise 8.13

    #include <stdio.h>
    #include <string.h>
    #include <conio.h>

    void PrintLatinWord(char *word);
    int main(void)

    char sentance[255];
    char array1[255];
    char array2[255];
    char *delimiters=array1;
    char *token=array2;
    strcpy(delimiters, " ");

    printf("Please enter a sentance to be translated:\n");

    token = strtok(sentance, delimiters);

    while (token != NULL)
    token = strtok(NULL, delimiters);
    printf("%p\n", token);


    return 0;

    void PrintLatinWord(char *word)

    int flag=0, i=0;
    char temp=word[0];
    char *tptr=&temp;

    //shifts every character one over to the left

    if (word[i]=='\0')

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


    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...

  2. #2
    Registered User
    Join Date
    Jan 2002
    your problem is here


    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

  3. #3
    Registered User
    Join Date
    Jan 2002
    thanx, ill try that out for sure today...

  4. #4
    Nice to know you occasionally ask someone other than me for help :p

    Here's my working routine:

    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
    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.

    EDIT: Bloody vBcode read my index as an italics tag. Ignore the spaces
    Last edited by Citizen Bleys; 01-28-2002 at 01:58 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM