Reversing Strings

• 03-22-2005
Halo
Reversing Strings
Hello Again,

Does anyone have a piece of code that can flip a string around or know how an easy way to do this? I am working on a special calculator that has will compute a variation of two's compliment. I am not good at all with string manipulation.

An example of this would be:

INPUT: halo
OUTPUT: olah

Thank You!
• 03-22-2005
IfYouSaySo
Try the reverse standard algorithm:

Code:

```# include <string> # include <algorithm> # include <iostream> using namespace std; int main() {         string s = "halo";         cout << "String is: " << s << endl;         std::reverse<string::pointer>(s.begin(), s.end());         cout << "Reverse string is: " << s << endl;         return 0; }```
• 03-22-2005
7stud
You can do it this way too:
Code:

```string str = "halo"; string reversed; for(int i = str.length() - 1; i >= 0; i--) {         reversed += str[i]; } cout<<reversed<<endl;```
• 03-18-2006
stevedawg85
I know this is an old topic, but why create a new topic when this is exactly what I need.

Ok, can you comment (//) on the piece of coding 7stud wrote^^. Im trying to understand this but how does the computer know to place character X into the Y position.

thnx!!!
• 03-18-2006
dwks
You're not supposed to post in threads over 2 weeks old. :)

7stud's code just takes the last character of str and adds it to reversed, making it the first character. Then it takes the next-to-last character and adds it to the end of reversed, which makes it the second character. And so on until the beginning of the string.
• 03-18-2006
jlf029
if you are using char[] arrays, you can simply swap the elements
• 03-18-2006
major_small
a slightly more efficient method is to also use a reverse-iterator, which starts at the end of the new string and the beginning of the old string.

In each iteration, the following happens:
• The first character of the source string is written to the last character of the target string
• The last character of the source string is written to the first character of the target string
• The forward iterator is increased by one
• The reverse iterator is decreased by one

I can see how this could be confusing to read, so here's a bit of code to show you more:
Code:

```#include <iostream>        //for standard console I/O #include <cstring>        //for strlen() int main() {         char source[]="dlroW olleH";        //the source string         char target[]="dlroW olleH";        //the target string         register short int f;                //the forward iterator         register short int r;                //the reverse iterator         /*         * this line gets the length of the source string.  In this case, I         * made sure both strings were the same length.  You could, after this         * point, allocate a target string just for the purposes of copying.         */         register const short int LEN=strlen(source);         /*         * This is the "magic loop".  What it does is set sthe forward iterator         * to zero, the reverse iterator to the last index to be copied, and         * loops until they are of equal size (or somehow they pass eachother).         * In each iteration, it increases the forward iterator and decreases         * the reverse iterator.  The iterators work their ways from the ends         * of the string to the center.         */         for(f=0,r=LEN-1; f<r; f++,r--)         {                 /*                 * The first line copies the first character in source to be                 * copied into the last character in target to be written.                 * The second line does the same, except it uses the last char                 * in source to be read to write into the first position of                 * target to be written.                 */                 target[r]=source[f];                 target[f]=source[r];         }         std::cout<<target<<std::endl;        //output the target string         return 0;                        //be nice to your creator. }```