pigLatin, code prints funny characters

This is a discussion on pigLatin, code prints funny characters within the C Programming forums, part of the General Programming Boards category; Hi, This is a program which converts an english phrase to pigLatin. The algorithm is to take out the first ...

  1. #1
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020

    pigLatin, code prints funny characters

    Hi,

    This is a program which converts an english phrase to pigLatin. The algorithm is to take out the first letter of the word and puts it after the last character of the english word (ignoring '\0' ) plus adding 'ay' after that. Somehow, it doesn't work. Pls direct. Thnx in advance.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    void printLatinWord( char *s );
    
    int main()
    {
       char engPhrase[] = "This is an english sentence.";
    
       printf( "Original string: \n%s\n\n", engPhrase );
    
       printf( "String converted to pigLatin: \n" );
    
       printLatinWord( engPhrase );
    
       system( "PAUSE" );
       return 0;
    }
    
    void printLatinWord( char *s )
    {
       char *tokenPtr;
       char temp[ 4 ];
       char b[ 3 ] = "ay";
    
       tokenPtr = strtok( s, " " );
    
       while ( tokenPtr != NULL ) {
          temp[ 0 ] = tokenPtr[ 0 ];
          strcat( temp, b );
          strcat( tokenPtr, temp );
          printf( "%s\n", tokenPtr );
          tokenPtr = strtok( NULL, " " );
       }
    }

  2. #2
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    >>> temp[ 0 ] = tokenPtr[ 0 ];

    This line copies the first character of one string to the first character of the other, but the destination string is not correctly terminated, it contains the character you have copied plus any other rubbish that was there before. You need to put a '\0' character in position temp[1] before calling strcat().

    You'll then discover you have another problem!
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  3. #3
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    yea it still prints funny characters. How to fix THAT?

    thnx

  4. #4
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    >>> tokenPtr = strtok( NULL, " " );

    That NULL looks a bit suspicious...
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  5. #5
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    nope, i checked the book, thats the right way. What are the other problems thats preventing the pro to work correctly?

  6. #6
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    When you first call strtok() it sets up a static context. I don't think you can muck about with things after this has happened.

    Code:
    void printLatinWord( char *s )
    {
       char *tokenPtr;
       char temp[ 4 ];
       char b[ 3 ] = "ay";
    
       tokenPtr = strtok( s, " " );
       tokenPtr = strtok( s, " " );
    
       while ( tokenPtr != NULL ) {
          temp[ 0 ] = tokenPtr[ 0 ];
          strcat( temp, b );
          strcat( tokenPtr, temp );
          printf( "%s\n", tokenPtr );
          tokenPtr = strtok( NULL, " " );
       }
    }
    This, for example, correctly chooses "This" as the first word and "is" for the second. The way you have it, after processing the first word, (This -> ThisTay), strtok() returns "ay".
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  7. #7
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    why strtok returns "ay" ? It returns the next word after the delimiter whitespace doesn't it?

  8. #8
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    It should, but if you run your code in the debugger, you will see that it is not, (or at least, it is not with my compiler). I, as I mentioned before, suspect your have done something to the static context that strtok() maintains.

    I tend not to use the functions that maintain static context, (not just because you can't see what is going on, they are, for example, not thread safe).
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  9. #9
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    what exacltly should i do to the code, like which part do i edit?

  10. #10
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    Can someone please post a solution to my problem? I am pretty new with string manipulations. Maybe the fix to the problem is just a line or two but i dunno it. Pls direct.

    thnx

  11. #11
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Originally posted by Nutshell
    Can someone please post a solution to my problem? I am pretty new with string manipulations. Maybe the fix to the problem is just a line or two but i dunno it. Pls direct.

    thnx
    Code:
    #include <stdio.h>
    #include <string.h>
    
    void printLatinWord( char *s );
    
    int main()
    {
       char engPhrase[] = "This is an english sentence";
    
       printf( "Original string: \n%s\n\n", engPhrase );
    
       printf( "String converted to pigLatin: \n" );
    
       printLatinWord( engPhrase );
    
       //system( "PAUSE" );
       return 0;
    }
    
    void printLatinWord( char *s )
    {
       char *tokenPtr;
       char b[] = "ay";
    
       tokenPtr = strtok( s, " " );
       while (tokenPtr) {
          printf( "%s%s ", tokenPtr,b );
          tokenPtr = strtok( NULL, " " );
       }
    }
    How's that?

  12. #12
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Mind you...I dont know pig latin..........do you just add 'ay' on the end?
    Last edited by Fordy; 01-23-2002 at 04:49 AM.

  13. #13
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    I'm informed that in Piglatin you take the first letter and put it on the end and follow with 'ay' (What is this crap!!??!?!?!)

    If so ...try;

    Code:
    #include <stdio.h>
    #include <string.h>
    
    void printLatinWord( char *s );
    
    int main()
    {
       char engPhrase[] = "This is an english sentence";
    
       printf( "Original string: \n%s\n\n", engPhrase );
    
       printf( "String converted to pigLatin: \n" );
    
       printLatinWord( engPhrase );
    
       //system( "PAUSE" );
       return 0;
    }
    
    void printLatinWord( char *s )
    {
       char *tokenPtr;
       char b[] = "ay";
       char first;
    
       tokenPtr = strtok( s, " " );
       while (tokenPtr) {
          first = tokenPtr[0];
          ++tokenPtr;
          tokenPtr[strlen(tokenPtr)] = '\0';
          printf( "%s%c%s ", tokenPtr,first,b );
          tokenPtr = strtok( NULL, " " );
       }
    }

  14. #14
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    The point I was trying to make is demonstrated by Fordy's first program, (it may not do exactly what you want but...), you notice he does not interfere with the "str" static context. Change his routine to this...

    Code:
    void printLatinWord( char *s )
    {
       char *tokenPtr;
       char b[] = "ay";
    
       tokenPtr = strtok( s, " " );
       while (tokenPtr) {
          printf( "%s%s ", tokenPtr,b );
          strcat(tokenPtr,b);
          tokenPtr = strtok( NULL, " " );
       }
    }
    ... notice the call to strcat(), this program is now screwed.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  15. #15
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    ok so i understand now, i can't do anything with the token returned by strtok() right? New thing i learnt. At first i was trying to push everything ( first letter and "ay" ) into the string token and never thought of simply jsut printing it. Thnx guys, thats clever, or am i just stupid? dun say that....

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  2. HELP!!!!emergency ~expert please help
    By unknowppl in forum C Programming
    Replies: 1
    Last Post: 08-19-2008, 07:35 AM
  3. Replies: 10
    Last Post: 07-10-2008, 03:45 PM
  4. Obfuscated Code Contest: The Results
    By Stack Overflow in forum Contests Board
    Replies: 29
    Last Post: 02-18-2005, 04:39 PM
  5. Replies: 4
    Last Post: 01-15-2002, 11:04 PM

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