You are here:

C++/bout reverse text

Advertisement


Question
ok....i juz wanna ask u bout how to reverse the character,...dats all....ASAP.

Answer
I am sorry but your question makes no sense as it stands. You cannot reverse a character, unless you wish to reverse the order of the bit values that represent it. Did you mean to ask about reversing the order of characters in a string of characters? If so read on...

Please note that as I suspect this is an exercise I am not going to show much code.

If you think about a string as a number of blocks with letters on them (as children might play with) placed in a line then you can move each letter block (i.e. character) from the back of the line (i.e. string) to the front, working back down the line, or the other way around. Alternatively you can swap each character from the back with one from the front.

Which technique you use depends on whether the reversed string is to occupy the same space as the original or be a new string. If you are overwriting the original string with its reverse then swapping is the way to go. Otherwise write the characters of the new string by appending characters from the back of the original string, working your way toward the front of the original string.

Again the letter blocks analogy works quite well. If you have a line of blocks then swapping one block with another does not require more space, it just re-arranges the order of the letters. However of you look at each block in the line starting from the back of the line and place a block with the same letter as each one under the existing line, starting under the first block in the original line then the second etc. you build a new line of blocks with letters arranged in the reverse order of the original line of blocks.

Note that this shows a difference between real things and computers: in computers we tend to copy values rather than move them. So when I say append a character in one string to the end of another string what I really mean is append a character that is a _copy_ of the value of the character in the first string. The first string is not changed. On the other hand if I said append a letter block from one line of blocks to another line of blocks we would tend to pick up the block from the first line of blocks and move it to the end of the second line of blocks, changing the first line of blocks.

Luckily for us reversing the order of sequences of things such as strings of characters is so common that the C++ standard library has algorithms to do just this. They are called reverse and reverse_copy (or, more correctly std::reverse and std::reverse_copy as the names of most C++ standard library things are in the namespace std). The reverse algorithm reverses a sequence so the same sequence becomes reversed and reverse_copy makes a reversed copy of one sequence into another. We can use it with the standard C++ string like so:

   #include <string>
   #include <algorithm>
   #include <iostream>

   int main()
   {
       std::string theText
         ("The quick brown fox jumped over the lazy dog");
       
       std::cout << "Original text: " << theText << '\n';

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

       std::cout << "Reversed text: " << theText << '\n';
   }

Should you need to perform the reverse operation on a string without using library help then you will need to use a loop, calculate which character index values you need, what range of character indexes you need etc. For example if swapping values you only need to swap half the values in the string with the other half so looping from index 0 to less than half the string length will give the correct range and one set of index values to swap with. For each character index n, the index to swap with will be string length - n - 1. The - 1 is needed as C/C++ index from 0. So you swap character 0 with character string length - 1 (the last character's index), character 1 with character string length - 2 etc.

If you think about what I have written you should be able to write a string reversing program. However you will have to use your brain. Oh, and do not give up just because things do not work the first (or second or third) time. This is usual and is a good way to learn!  

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Ralph McArdell

Expertise

I am a software developer with more than 15 years C++ experience and over 25 years experience developing a wide variety of applications for Windows NT/2000/XP, UNIX, Linux and other platforms. I can help with basic to advanced C++, C (although I do not write just-C much if at all these days so maybe ask in the C section about purely C matters), software development and many platform specific and system development problems.

Experience

My career started in the mid 1980s working as a batch process operator for the now defunct Inner London Education Authority, working on Prime mini computers. I then moved into the role of Programmer / Analyst, also on the Primes, then into technical support and finally into the micro computing section, using a variety of 16 and 8 bit machines. Following the demise of the ILEA I worked for a small company, now gone, called Hodos. I worked on a part task train simulator using C and the Intel DVI (Digital Video Interactive) - the hardware based predecessor to Indeo. Other projects included a CGI based train simulator (different goals to the first), and various other projects in C and Visual Basic (er, version 1 that is). When Hodos went into receivership I went freelance and finally managed to start working in C++. I initially had contracts working on train simulators (surprise) and multimedia - I worked on many of the Dorling Kindersley CD-ROM titles and wrote the screensaver games for the Wallace and Gromit Cracking Animator CD. My more recent contracts have been more traditionally IT based, working predominately in C++ on MS Windows NT, 2000. XP, Linux and UN*X. These projects have had wide ranging additional skill sets including system analysis and design, databases and SQL in various guises, C#, client server and remoting, cross porting applications between platforms and various client development processes. I have an interest in the development of the C++ core language and libraries and try to keep up with at least some of the papers on the ISO C++ Standard Committee site at http://www.open-std.org/jtc1/sc22/wg21/.

Education/Credentials

©2016 About.com. All rights reserved.