Reverse words in a string without affecting space
Use compiler warnings

You should always enable compilers warning in c++. For your code, clang &amp; g++ gives the following warnings (live demo):

prog.cc:14:29: warning: multi-character character constant [-Wmultichar]
        if(s[i]!=' '||s[i]=='/0'){
                            ^
prog.cc:13:19: warning: comparison of integers of different signs: 'int' and '...' (aka 'unsigned long') [-Wsign-compare]
    for(auto i=0;i&lt;s.size();){
                 ~^~~~~~~~~
prog.cc:14:27: warning: result of comparison of constant 12080 with expression of type '...' (aka 'char') is always false [-Wtautological-constant-out-of-range-compare]
        if(s[i]!=' '||s[i]=='/0'){
                      ~~~~^ ~~~~


The ones about s[i]=='/0' are especially concerning. You probably meant s[i]=='\0'. I don't know if you want to handle other possible separators of a string (like tabs, line feeds). If it's the case, you should have a look at std::isspace or std::isblank.

The one about the for suggests that the type int (deduced for auto) might not be the most suited as a string/vector index. According to this post, the most portable index type to use would be std::string::size_type.

Avoid std::string copy

Your code is performing a lot of string construction / copy / concatenation. They all can cause memory allocation/deallocation, which can be quite costly compared to the rest of your code. You should be able to avoid most of them.

Output: generate the result directly in a single std::string


You know exactly the size of the output string re (same size as input). You can preallocate it with std::string::reserve.
Pass a reference to re to void oreverse(std::string&amp; output,/*InputType*/ input), and directly push_back the characters of the input in reverse order.


Input: work directly on the input string


pass your input string by (const) references in both functions: const std::string&amp;.
avoid std::string::substr:


C++17: you can use std::string_view instead of std::string. It's substr method won't do any copy.
Pre-C++17: pass iterators to the input string to oreverse. Or a (const) reference to the input string, with start/end indices.



Use "static" for global functions when applicable

Declaring a global function as "static" indicates to your compiler that your function is used only in the current translation unit (current *.cpp file). The compiler can use this information to do better inlining decisions (see for example gcc/g++: -finline-functions-called-once option).