(Tokenizing and Reversing a Sentence) Write a program that inputs a line of text, tokenizes the line with function strtok and outputs the tokens in reverse order.

What will be an ideal response?

```
#include
#include
using namespace std;

void reverseTokens( char * const ); // prototype

int main()
{
const int SIZE = 80;
char text[ SIZE ];

cout << "Enter a line of text:\n";
cin.getline( text, SIZE );

// call to function reverseTokens
reverseTokens( text );
cout << endl;
return 0; // indicate successful termination
} // end main

// function to reverse the individual tokens
void reverseTokens( char * const sentence )
{
char *pointers[ 50 ]; // array to store entire sentence
char *temp; // store each word

int count = 0; // serve as array counter

// call function strtok to take first word out of sentence
temp = strtok( sentence, " " );

// while temp is not empty
while ( temp )
{
// add the word into the array
pointers[ count++ ] = temp;

// get each subsequent word from the sentence
temp = strtok( 0, " " );
} // end loop

cout << "\nThe tokens in reverse order are:\n";

// loop through the array backwards
for ( int i = count - 1; i >= 0; i-- )
cout << pointers[ i ] << ' ';
} // end function reverseTokens
```
Enter a line of text:
twinkle twinkle little star
The tokens in reverse order are:
star little twinkle twinkle

Computer Science & Information Technology

You might also like to view...

________ can be used like bookmarks in a procedure

Fill in the blank(s) with correct word

Computer Science & Information Technology

Excessive amounts of broadcasts on a network are called a broadcast storm. True or False?

Indicate whether the statement is true or false

Computer Science & Information Technology