Anyone can help me to write a code in C++ to reverse a string.
Example:
i/p= " string with space "
o/p= " ecaps htiw gnirts "

Better if we use function or class.
Thanks in advance

#include <string>
#include <algorithm>

int main(){
  std::string text = "reversi mei!";
  std::reverse(text.begin(), text.end());
}

Sorry, I forgot to mention that the program ask the user to input the string that needed to be reversed. Also without using any in-build string function. i.e reverse etc.

Create an index at both ends of the string and swap characters, moving the indices closer until they meet. When they meet (or pass), the string will be completely reversed.

Thanks Narue for your kind help. I have one more doubt, how can I create the end index of the string?

It depends on which string type you use. For std::string the last index is s.size()-1 . For C-style strings, you'd need to count up to the null terminator with something like strlen: strlen(s)-1 .

try it in c first, just for fun. Here is something to get you started, complete the function, use algorithm described by Narue..

char *reverse( char *input)
{



}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.