Manipulating a String by Reversing the Word Order Help

Hello. Can someone help me with this problem below? I'm currently studying how to manipulate a string by myself and can someone on how to reverse the order of
the word and explain to me how you did it? I also need some references in order to master the string manipulation.

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>

using namespace std;

int main()
{
    cout<<"hello world";

    return 0;
}

//hello world
//The output should be: //dlrow olleh 
Last edited on
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
34
35
36
37
38
39
40
41
#include <iostream>
#include <string>
#include <ranges>  // c++20

int main()
{
   std::string str { "C++ is Cool!" };

   // old school for loop
   for (size_t i { }; i < str.size(); ++i) { std::cout << str[i] << ' '; }
   std::cout << '\n';

   // for loop using const iterators
   for (auto itr { str.cbegin() }; itr != str.cend(); ++itr) { std::cout << *itr << ' '; }
   std::cout << '\n';

   // https://en.cppreference.com/w/cpp/language/range-for
   for (const auto& itr : str) { std::cout << itr << ' '; }
   std::cout << "\n\n";

   // old school reverse for loop
   for (size_t i { str.size() }; i > 0; --i) { std::cout << str[i - 1] << ' '; }
   std::cout << '\n';

   // reverse for loop with const iterators
   for (auto itr { str.crbegin() }; itr != str.crend(); ++itr) { std::cout << *itr << ' '; }
   std::cout << '\n';

   // https://www.fluentcpp.com/2020/02/11/reverse-for-loops-in-cpp/
   for (const auto& itr : str | std::views::reverse) { std::cout << itr << ' '; }
   std::cout << "\n\n";

   // range-based for loops work with regular arrays as well
   int arr[ ] { 5, 10, 15, 20, 25 };

   for (const auto& itr : arr) { std::cout << itr << ' '; }
   std::cout << '\n';

   for (const auto& itr : arr | std::views::reverse) { std::cout << itr << ' '; }
   std::cout << '\n';
}
C + +   i s   C o o l !
C + +   i s   C o o l !
C + +   i s   C o o l !

! l o o C   s i   + + C
! l o o C   s i   + + C
! l o o C   s i   + + C

5 10 15 20 25
25 20 15 10 5
If'n you want to alter the string by reversing it, std::reverse in <algorithm>:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <string>
#include <algorithm> // std::reverse

int main()
{
   std::string str { "Hello World" };

   std::cout << str << '\n';

   std::reverse(str.begin(), str.end());

   std::cout << str << '\n';
}
Hello World
dlroW olleH
Reversing the word order is just one step more: find where the words begin and end and reverse each individual one.


    Hello world!
    
↓ reverse string
    !dlrow olleH
    
↓ reverse first word
    world! olleH
    
↓ reverse second word
    world Hello


Alternately, you can split the string into words (in any way you like) and then rebuild the string using the reversed result.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <sstream>
#include <string>
#include <vector>

std::deque <std::string> split( const std::string& s )
{
  std::vector <std::string> result;
  std::istringstream ss( s );
  std::string z;
  while (ss >> z) result.push_back( z );
  return result;
}

int main()
{
  auto strings = split( "Hello world!" );
  for (auto n = strings.size();  n--; )
    std::cout << strings[n] << " ";
  std::cout << "\n";
}
world! Hello


If you are using C++20, you can reverse things with ranges:

1
2
3
4
  for (auto s : strings | std::views::reverse)
  {
    std::cout << s << " ";
  }

If you are living before C++20 (like me) you can use the handy-dandy reverse range helper:

1
2
3
4
5
6
7
8
9
10
11
12
template <typename Iterable>
struct reverse
{
  const Iterable & a;
  explicit reverse( const Iterable & iterable ) : a{ iterable } { }
  
  Iterable b;
  explicit reverse( Iterable && iterable ) : a{b}, b{ std::move( iterable ) } { }
  
  auto begin() const { using std::rbegin; return rbegin( a ); }
  auto end()   const { using std::rend;   return rend  ( a ); } 
};
1
2
3
4
for (auto s : reverse( strings ))
{
  std::cout << s << " ";
}

Enjoy!
Topic archived. No new replies allowed.