Solved

How to reverse a string

Posted on 2003-12-04
13
279 Views
Last Modified: 2010-04-01
I have the following codes:

...
...
string line("this is a test");
...
...

how can I reverse this string line?

P.S.:
No c-function such as strrev().
0
Comment
Question by:chenwei
13 Comments
 
LVL 1

Expert Comment

by:Hoegje
Comment Utility
Homework maybe ?

How do you do something like that ? Think a bit for yourself. Take small steps at a time.. There are several ways to do this, so pick any of them..
0
 
LVL 17

Expert Comment

by:rstaveley
Comment Utility
You might consider reverse iterators (rbegin() and rend()). Enough said, assuming this is HW.
0
 
LVL 7

Expert Comment

by:kohashi
Comment Utility
This sounds like homework so I will provide psuedo code to do it an easy fashion:

get initial string length
create a string of the same length
run a loop that counts down from length to zero
inside the loop place the original string[x] (assuming x is the decrementing variable) inside of newstring[length-x]

once the loop completes, you will have your string in reverse.  You may need to make it length-x-1 or add one and other such minute changes to make sure you dont exceed the allotted space and it gets put in the proper place.

hope that helps,
-kohashi
0
 
LVL 8

Expert Comment

by:mnashadka
Comment Utility
There's also a reverse function in STL.  This will work on string iterators.
0
 
LVL 1

Expert Comment

by:skp23
Comment Utility
You don't really need the new string.
You can simply swap the first and last, then the next pair and so on.
0
 
LVL 1

Expert Comment

by:zania22
Comment Utility
here is the code;

string line("this is a test");

//get the length of the string
int length = line.length();

//just print out backwards with a for loop
int i;
for(i=length-1;i>=0;i--){
    cout<<line[i];
}

cout<<endl;

good luck!
-zania22

0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 17

Expert Comment

by:rstaveley
Comment Utility
zania22, you just did what Hoegje, rstaveley, kohashi, mnashadka, skp23 and many other EE members, who refrained from commenting, carefully avoided doing. Woops.
0
 

Expert Comment

by:t_chinhoe
Comment Utility
May be chenwei can use other methods doing it... like recursive
0
 
LVL 2

Accepted Solution

by:
xssass earned 20 total points
Comment Utility
the loop of zania22 is all right, but if it is a C++ style String, it starts counting from 1.

So it should be:

#include <string.h>
#include <iostream.h>

String line="This is a testline";

for (int i=line.Length(); i>=1; i--){
  cout << line[i];
}
cout << endl;
0
 

Author Comment

by:chenwei
Comment Utility
Thanks for the info from all sites. But truely to say, the method you gave me is not bad but is a very normal one which can be found out everywhere.

Originally what I want is to look for a simple function such as strrev() in C.

0
 
LVL 17

Expert Comment

by:rstaveley
Comment Utility
Here's the reverse iterators:
--------8<--------
#include <iostream>
#include <string>

int main()
{
      std::string test("this is a test");
      std::string reversed_test(test.rbegin(),test.rend());
      std::cout << reversed_test << '\n';
}
--------8<--------
I don't expect that it will score you points in a homework assignment, though. They's be more likely to be looking for loops etc.
0
 
LVL 8

Expert Comment

by:mnashadka
Comment Utility
reverse is found in <algorithm>
0
 
LVL 17

Expert Comment

by:rstaveley
Comment Utility
> They's

They'd

[Nearly mis-typed that the same way in my correction too. Must push my keyboard to the left a bit.]
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now