Reversing words

Hello, How can I reverse words in sentence? I try to do it with strtok.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

int main()
{
    char a[100];
    char * str;
    printf("Enter sentence: ");
    fgets(a,100,stdin);
    printf("%s",a);
    int i=0;

    char reviser[]=" ";
    str = strtok(a,reviser);

    while(str != NULL )
    {
        printf( "%s\n", str );
        str = strtok( NULL, reviser );
    }


     return 0;
}
Last edited on
Have you considered placing the "tokens" into an array and then printing that array backwards?
Yes, but I am wondering how to put "tokens" into array.
Suppose you have an array called 'array' and a token called token, with the next available spot in the array is array[3]

All you do is write array[3] = token;
Is this suitable for you?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/*  
 *  Reverse words in a sentence
 */ 

#include <iostream>
#include <cstring>

int main(){
  // normal string
  std::string n = "Join the NAVY";
  // reversed string
  std::string r;

  // for to loop through normal string from end to begin
  for(int i = (n.size() - 1); i >= 0; i --){
    r += n[i];  // appends char at s[i] to r string
  }
  std::cout << n << '\n';  // normal string
  std::cout << r << '\n';  // reversed string
  
  return 0;
}


$ ./example
Join the NAVY
YVAN eht nioJ


Greetings!
No, should be NAVY the Join
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <sstream>
#include <string>
#include <stack>
using namespace std;

int main()
{
   string sentence, word;
   cout << "Input sentence: ";    getline( cin, sentence );
   stack<string> S;
   for ( istringstream ss( sentence ); ss >> word; ) S.push( word );
   for ( ; !S.empty(); S.pop() ) cout << S.top() << ' ';
}

Input sentence: Join the NAVY
NAVY the Join 
Thanks but I see for the first time stack,ss,S.empty etc I have never used before
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

int main()
{
    char a[100];
    char * str;
    printf("Enter sentence: ");
    fgets(a,100,stdin);
    printf("%s",a);


    char reviser[]=" ";
    str = strtok(a,reviser);
    int i=0;
    int j;
    char *array[100];
    while(str != NULL )
    {
        array[i]=str;
        printf( "%s\n", str );
        str = strtok( NULL, reviser );

        i++;
    }
   for(j=i-1; j>=0; j--)
   {
       printf("%s\n",array[j]);
   }

     return 0;
}

I have something like that
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <sstream>
#include <string>
using namespace std;

void print( istream &in )
{
   string word;
   if ( in >> word )
   {
      print( in );
      cout << word << '\n';
   }
}

int main()
{
   string sentence, word;
   cout << "Input sentence: ";
   getline( cin, sentence );
   istringstream ss( sentence );
   print( ss );
}

Input sentence: Join the NAVY
NAVY
the
Join
I have something like that

Okay, does that solve your problem?
Okay, does that solve your problem?

Yes
@asxxx oh, sorry, I misunderstood the problem.
"Let's go by parts..." Jack the Ripper said...

In line 11 @lastchance declares a stack of string elements and calls it S. A stack is like an "enhaced array". It has very useful functions like push(), pop(), top() (he use they later)... See the links below:
http://www.cplusplus.com/reference/stack/stack/
https://www.geeksforgeeks.org/stack-in-cpp-stl/

And in line 12 he creates ss (object of class istringstream) and fill it with the content of the string sentence. Then extracts word by word from ss to word (I asume (need to read more) that the operator >> divide the string when find a space and returns some kind of false when reaches the end of the string so the for loop ends) and then push word content into S stack.
http://www.cplusplus.com/reference/sstream/istringstream/
http://www.cplusplus.com/reference/sstream/istringstream/istringstream/

Finally en line 13 he shows the element on top of the stack, while it isn't empty and then removes it leaving the previous word on the top.

And voilĂ !

Thank you @lastchance... I learned a lot with your code.
Err right! Glad someone's happy, even if it wasn't the OP!

Belated Happy Christmas!
Topic archived. No new replies allowed.