1
votes

include stdio.h

include stdlib.h

include string.h

#include <conio.h>

#define SIZE 40

int main( void )
{
    char str[ SIZE ];
    char strrev[ SIZE ];
    char *temp1;
    char *temp2;
    int turn = 0;

    printf( "Enter line of text: " );
    fgets( str, SIZE - 1, stdin );

    temp1 = strtok( str, " " );
    temp2 = strtok( NULL, " " );
    strcat( strrev, temp2 );
    strcat( strrev, temp1 );

The problem is the condition inside while:

    while( temp1 != NULL && temp2 != NULL ){
           if( turn == 0 ){
               temp1 = strtok( NULL, " " );
               strcat( strrev, temp1 );
               strcat( strrev, temp2 );
               turn = 1;

               }//end if
           else{
                temp2 = strtok( NULL, " " );
                strcat( strrev,temp2 );
                strcat( strrev, temp1 );
                turn = 0;

                }//end else

           }//end while

    printf( "\nThe reversed sentence is: %s", strrev );

    getche();
    return 0;
}//end function main

Since eventually either of the strings temp1 or temp2 are going to get a NULL why doesn't the loop function correctly ?

3
Can you please specify what you mean by "loop doesn't function correctly" - Arun A S
When I run it my program crashes and I tested it so I found that the problem was Inside the condtition I set in while - Shinz6
You are strcatting twice for each token you etract. Your target buffer might overflow. Also, for strcat to work, you must begin with a null-terminated string. In your code strrev isn't initialised. - M Oehm
I used strcat twice because I wanted to reverse a sentece like this "This is a dog" into "dog a is this" - Shinz6
I don't think that you program can reverse a sentence. You could push the tokens onto a stack and then print them i reverse order. - M Oehm

3 Answers

1
votes

You can use recursion to reverse the sentence and sscanf to split the string:

#include <stdio.h>

void recursive(const char *s)
{
  char b[100];
  int n;

  if (sscanf(s, "%99s%n", b, &n) == 1)
    recursive(s + n), printf(" %s", b);
}

int main()
{
  recursive("foo bar baz");
  return 0;
}
1
votes

I would not use strtok because 1) it destroys the original string and you may not use it with string literals and 2) the number of spaces between the words in the string will be broken.

I would use the following approach

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

int main(void) 
{
    char *s = "This is a dog";
    size_t n = strlen( s );
    char t[n + 1];

    char *p = t;
    char *q = s + n;

    while ( q != s )
    {
        while ( q != s && isblank( *( q - 1 ) ) ) *p++ = *--q;
        char *tmp = q;
        while ( tmp != s && !isblank( *( tmp - 1 ) ) ) --tmp;
        memcpy( p, tmp, q - tmp );
        p += q - tmp;
        q = tmp;
    }

    *p = '\0';

    puts( s );
    puts( t );

    return 0;
}

The program output is

This is a dog
dog a is This
0
votes

Thanks for the help, I thought of solving it like this:

#include <stdio.h>
#include <string.h>
#include <conio.h>
#define SIZE 40

void reverse( char sentence[], char *token2 );

int main( void )
{
    char sentence[ SIZE ] = "This is a dog";
    char *token1;
    char *token2;
    token1 = strtok( sentence, " " );
    reverse( sentence, token2 );
    printf( "%s", token1 );

    getche();
    return 0;
}//end function main

void reverse( char sentence[], char *token2 )
{

     if( token2 == NULL )
         return;

     else{
          token2 = strtok( NULL, " " );
          reverse( sentence, token2 );

          if( token2 != NULL ){ 
              printf( "%s ", token2 );

              }//end if    

         }//end else

}//end function reverse