Thread: Parsing First Characters From Sequence of Words

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    36

    Parsing First Characters From Sequence of Words

    The following snippet compiles gracefully however a windows dialog pops up with "illegal operation" when executed. I suspect the code is troublesome but am at a lost since it compiles.

    I'm using strtok to parse each word using a space as the delimitor for a sequence of words. I'm attempting to loop each word and parse the first character from each word and append it onto the string output.

    Code:
        /* Re-Arrangement */
        p = strtok(code, delimitor);
        strncpy(output, p, 1);
    
        while( p != NULL )
        {
            /* advance each token and split first char onto new string */
            p = strtok( NULL , delimitor );
            strcat(output, strncpy(temp, p, 1));
        
        }

  2. #2
    Registered User
    Join Date
    Feb 2008
    Posts
    36
    Code:
    /* Re-Arrangement */
        p = strtok(code, delimitor);
        strncpy(output, p, 1);
    
        while(1)
        {
            /* advance each token and split first char onto new string */
            p = strtok( NULL , delimitor );
            if( p != NULL )
                strcat(output, strncpy(temp, p, 1));
            else
                break;
    
        }
    
        printf(output);
    I've altered the code so it executes without error however the output is gibberish.

    NeܾÜÉ·'wܾÜÉ·'tܾÜÉ·'IܾÜÉ·'sܾÜÉ
    etc....

    The first few letters should be New etc, why is this happening.

    The following is the original input.

    News eight weather tonight: Increasing snow. Unexpected precipitation smothers eastern towns. Be extremely cautious and use snowti
    res especially Heading east. The highway is not knowingly slippery. Highway evacuation Is suspected. Police report emergency situations in downtown ending near
    tuesday.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > printf(output);
    NEVER do this - use user input as the control string of printf. If there is a % in there, you're hosed.

    How did you declare your strings 'output' and 'temp' ?
    Are they allocated sufficient space for example?

    Note that strncpy does NOT guarantee a \0 at the end of whatever it produces, but your code may be assuming this.

    I would suggest you replace your while(1) with a for loop which subscripts your output array. At least then you'll have some control over what happens when the array is full.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Feb 2008
    Posts
    36
    Code:
    char code[] = ".kmpeqgf dmqz szupzq ziafziap zu ezaufmgfue kozqsdqyq fdabqd qouxaB .pqfoqbege eU zaufmgomhq kmitsuT .kdqbbuxe kxszuiazw faz eu kmitsut qtF .femq szupmqT kxxmuoqbeq eqdufiaze qeg pzm egaufgmo kxqyqdfjq qN .eziaf zdqfemq edqtfaye zaufmfubuoqdb pqfoqbjqzG .iaze szuemqdozU :ftsuzaf dqtfmqi ftsuq eiqZ";
    char output[sizeof(code)];
    char temp[sizeof(code)];
    The above are the concerned variables. I don't understand where the gibberish is coming from.

    News eight weather tonight: Increasing snow. Unexpected precipitation smothers eastern towns. Be extremely cautious and use snowti
    res especially Heading east. The highway is not knowingly slippery. Highway evacuation Is suspected. Police report emergency situations in downtown ending near
    tuesday.
    NeܾÜÉ·'wܾÜÉ·'tܾÜÉ·'IܾÜÉ·'sܾÜÉ
    Last edited by Cero.Uno; 04-19-2008 at 11:53 PM.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Something wrong with doing say
    Code:
    #define MAX_SIZE 100
    char code[MAX_SIZE];
    char output[MAX_SIZE];
    char temp[MAX_SIZE];
    ?

    Like I said about the \0 and strncpy, your temp array for all it's size only needs to be
    Code:
    char temp[2] = { 0, 0 };
    You then replace the first character with strncpy
    Then you can concatenate a single char to the output.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Feb 2008
    Posts
    36
    This is an exercise, the output SHOULD be Newt Is Upset Because He Thinks He Is President hence the first letters of the code, I cant crack this damn puzzle.

  7. #7
    Registered User
    Join Date
    Feb 2008
    Posts
    36
    thanks Salem,

    changing

    Code:
    char temp[sizeof(code)];
    to

    Code:
    char temp[2] = { 0, 0 }; fixed the problem.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. HELP!!!!emergency Problem~expert please help
    By unknowppl in forum C++ Programming
    Replies: 9
    Last Post: 08-21-2008, 06:41 PM
  2. Replies: 10
    Last Post: 07-10-2008, 03:45 PM
  3. parsing words out of a string?
    By Syneris in forum C++ Programming
    Replies: 11
    Last Post: 02-02-2006, 11:22 AM
  4. %i or %d - where is the difference?
    By Sargnagel in forum C Programming
    Replies: 3
    Last Post: 11-12-2002, 03:21 PM
  5. I hate string parsing with a passion
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 03-19-2002, 07:30 PM