something is missing, and i just cant see it!!

This is a discussion on something is missing, and i just cant see it!! within the C Programming forums, part of the General Programming Boards category; Alright, heres the thing, this assignment for school is due in like 3 days, i have spent so much time ...

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    8

    Unhappy something is missing, and i just cant see it!!

    Alright, heres the thing, this assignment for school is due in like 3 days, i have spent so much time staring at my code and i dont know whats wrong!! the problem is in the function, i know that for sure, something about mabey using strlan...

    This is what i have so far... its almost finished, just a few small changes is all i need, and im sure its a totally obvious problem, but i only started learning C two and a half weeks ago, so were all going to place pity on the beginners, right?? haha...

    //Alicia Hogue
    //Chapitre 8
    //Exercise 8.13

    //this program takes an inputted sentance in english and
    //translates it to "pig latin". Taking the first letter of each word
    //place it at the end of each word in the sentance, followed
    //by "ay".

    #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");
    gets(sentance);

    token = strtok(sentance, delimiters);

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

    }

    return 0;
    }

    void PrintLatinWord(char *word)

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

    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 tp the end

    strcat(word,"ay");
    }

    oh, and if you see any other errors in the code you can feel free to point them out as well... thanx!

  2. #2
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Token is a pointer to the next token in the string. You can't just add some characters to this pointer (in PrintLatinWord). A better way to do this is to copy the new (Latin) word to a new buffer (lattinWord). Try something like this:

    char * PrintLatinWord(char *englishWord, char *lattinWord);

    int main(void)
    {
    char sentance[255];
    char delimiters[] = " ";
    char lattinWord[255];

    char *token = NULL;

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

    token = strtok(sentance, delimiters);

    while (token != NULL)
    {
    printf("Token = [%s]\n", token);
    printf("Translation = [%s]\n",PrintLatinWord(token, lattinWord));
    token = strtok(NULL, delimiters);
    getch();
    }

    return 0;
    }

    char * PrintLatinWord(char *englishWord, char *lattinWord)
    {
    strcpy(lattinWord, englishWord+1);
    lattinWord[strlen(lattinWord)+1] = 0;
    lattinWord[strlen(lattinWord)] = englishWord[0];
    strcat(lattinWord,"ay");
    return lattinWord;
    }

  3. #3
    Unregistered
    Guest
    Quote;
    >token = strtok(sentance, delimiters);

    I haven't tested this, but isn't strtok a little flakey when it comes to error handling (like, it just doesn't)

    If the user were to just hit enter (i.e. not enter anything for sentence) wouldn't this pack a sad.

    Maybe using something along the lines of
    if ((token = strtok(sentance, delimiters))==NULL){
    printf ("Sorry, invalid, please type a sentance\n");
    return;
    }
    would be a good plan.

    I could be way off base (especially with the code), but my point is I think it's probably a good idea to trap errors in strtok from the word go.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21