Reversing a string

This is a discussion on Reversing a string within the C++ Programming forums, part of the General Programming Boards category; I'm taking C++ and have no prior computer experience. . . so I'm in need of a lot of help, ...

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    3

    Reversing a string

    I'm taking C++ and have no prior computer experience. . . so I'm in need of a lot of help, if anybody could help me! I need to reverse a string that will be inputed by the computer user, so that means that the length of the string is undefined. How do I go about reversing it, since it has an undefined length? Can anybody help please!

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Search the forum. This question gets posted all the time.
    Code:
    void revstr( void )
    {
        char c;
        std::cin >> c;
        if( c != '\n' )
            return;
        revstr( );
        std::cout << c;
    }
    You likely will not want to use this for your homework example, unless you can actually explain to your instructor exactly what's happening. If you can't, then you'll want to search the forum for a "real answer".

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jun 2003
    Posts
    3

    Question

    But my teacher won't let us use code like strrev(). I have to use a for loop. I just don't get how to do that. Could someone offer some help.

  4. #4
    Rad gcn_zelda's Avatar
    Join Date
    Mar 2003
    Posts
    942
    well, what he showed you was function recursion and works quite well. It's not a built in function!

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well, if you are allowed to use some of the standard template library items, there are some very easy ways to do this. Also, you could create a stack and push each character on it, then pop them all back off once they hit enter or what not, and display them that way. Using STL, you could easily use a vector or something nifty to do this.

    However, I suspect they have something a bit more simplistic in mind. Something like:

    1) Declare a huge buffer.
    2) Start a counter.
    3) Read characters into the buffer, incrementing the counter.
    4) When they enter a new line (enter on the keyboard), stop reading into the buffer.
    5) Since the counter will point at the end of the buffer, start there, and decrement the counter, displaying the contents of the buffer in reverse.

    By "buffer", in this case, you'd just likely use an array.

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    If your interested in the stack method, I'd recommend an STL list.

    You can use the push_front(item), front(), and pop_front() methods to make it behave just like a stack.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  7. #7
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493
    Originally posted by quzah
    Search the forum. This question gets posted all the time.
    Code:
    void revstr( void )
    {
        char c;
        std::cin >> c;
        if( c != '\n' )
            return;
        revstr( );
        std::cout << c;
    }
    You likely will not want to use this for your homework example, unless you can actually explain to your instructor exactly what's happening. If you can't, then you'll want to search the forum for a "real answer".

    Quzah.
    shouldn't it be c == '\n'? Otherwise it would just return for any string...

  8. #8
    Registered User slaveofthenet's Avatar
    Join Date
    Apr 2003
    Posts
    80
    I'd suggest using the string library. Here's a basic program that utilizes it.
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
    	string word;
    	cout << "Enter a word: ";
    	cin >> word;
    	cout << "The length of " << word << " is: " << word.size() << '\n';
    	cout << "The first character is: " << word[0] << '\n';
    	cout << "The last character is: " << word[word.size()-1] << '\n';
    	return 0;
    }
    I'll leave the logic up to you.
    Detailed understanding of language features - even of all features of a language - cannot compensate for lack of an overall view of the language and the fundamental techniques for using it. - Bjarne Stroustrup

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by ygfperson
    shouldn't it be c == '\n'? Otherwise it would just return for any string...
    Yep. Typo. It had it right first, but I changed how the function worked. Then I didn't like it, so I changed the rest of the function back, and forgot that line.

    [edit]
    The code I was going to use was:
    Code:
    void revstr( void )
    {
        char c;
        std::cin >> c;
        if( c != '\n' )
            revstr( );
        std::cout << c;
    }
    But I didn't like it, because it would have output the newline character, which I didn't like.
    [/edit]

    Quzah.
    Last edited by quzah; 06-26-2003 at 10:13 PM.
    Hope is the first step on the road to disappointment.

  10. #10
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    I must say, that is quite clever. I would've gone straight for the loop.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  11. #11
    Registered User
    Join Date
    May 2003
    Posts
    148
    Code:
    #include <iostream>
    #include <string>
    #include <algorithm>
    
    using namespace std;
    
    int main()
    {
        string str;
        getline(cin,str);
    
        cout << str << endl;
    
        reverse(str.begin(),str.end());
    
        cout << str << endl;
    }

  12. #12
    Registered User
    Join Date
    Jun 2003
    Posts
    3

    Problem

    Visual C++ does not understand std::. It keeps poping up as an error when I compile. How do I fix that?

  13. #13
    Registered User
    Join Date
    May 2003
    Posts
    148
    >>Visual C++ does not understand std::.
    Install Service Pack 5
    http://msdn.microsoft.com/vstudio/do...es/sp/vs6/sp5/

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  2. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  3. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  4. problems with overloaded '+' again
    By Brain Cell in forum C++ Programming
    Replies: 9
    Last Post: 04-14-2005, 05:13 PM
  5. Reversing a String
    By ToasterPas in forum C++ Programming
    Replies: 10
    Last Post: 08-14-2001, 12:20 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21