Thread: Reversing Strings

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    52

    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!

  2. #2
    Registered User
    Join Date
    Aug 2002
    Location
    Hermosa Beach, CA
    Posts
    446
    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;
    }
    The crows maintain that a single crow could destroy the heavens. Doubtless this is so. But it proves nothing against the heavens, for the heavens signify simply: the impossibility of crows.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    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;
    Last edited by 7stud; 03-22-2005 at 11:15 PM.

  4. #4
    Registered User
    Join Date
    Mar 2006
    Posts
    36
    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!!!

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    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.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User
    Join Date
    Nov 2005
    Posts
    85
    if you are using char[] arrays, you can simply swap the elements

  7. #7
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    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.
    }
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 03:24 PM
  2. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 08:07 PM
  3. Reversing strings using strtok()
    By Sure in forum C Programming
    Replies: 5
    Last Post: 06-27-2005, 05:33 PM
  4. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  5. reversing strings
    By mcorn in forum C++ Programming
    Replies: 14
    Last Post: 10-13-2002, 01:30 AM