Thread: reverse a string:

  1. #1
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499

    reverse a string:

    I was browsing the web looking for simple yet fun programming challenges and crossed this one. I figured out how to reverse the string in place but I want it to read "blue is house the". I approached it in two ways for the heck of it. My idea was the second one, the first one I googled. I didn't know a simple rbegin() could do that, pretty neat.

    I found the question here.http://www.techinterview.org Does anyone know of any other sites where I can find more?

    Code:
    #include <iostream>
    #include <string>
    
    
    int main(int argc, const char * argv[])
    {
    
        std::string phrase = "The house is blue.";
        
        for (auto it = phrase.rbegin(); it!=phrase.rend(); ++it)
        {
            std::cout<<*it;
        }
        std::cout<<"\n";
        
        for (int i=0; i<phrase.length(); ++i)
        {
            std::cout<<phrase.at(phrase.length()-i-1);
        }
        
        
        return 0;
    }
    Last edited by jocdrew21; 01-12-2014 at 09:15 AM.

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    To reverse the string by words, you could perhaps use the string member function find_last_of() to search for spaces, and then use substr() to output the word. This will require two indices, one indicating the end of the word and the other the beginning.

    Also,

    argv should not be declared const (the strings it points to are modifiable).


    Don't declare argc and argv if you're not using them (this leads to unused parameter warnings ... haven't you turned up your warning level?).
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499
    I do not know how to do that. I will have to google it.
    Honestly I don't even know what
    Code:
    int main(int argc, const char * argv[])
    does. I understand main but not its parameters, my compiler always puts them there, I am using Xcode 4

  4. #4
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    argc is the number of command line words and argv is the array that stores each one.

    Consider this command :
    Code:
    ./regulus -np 1 -bl 10
    Here, argc is 5 (regulus, -np, 1, -bl, 10) and argv is regulus (0), -np (1), 1 (2), -bl (3), 10 (4).

    Normally, people just type void or leave what's in-between the parentheses blank.

  5. #5
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499
    whats the point of using them? So int
    Code:
     main(void){ }
    is fine? It will do the same thing?

  6. #6
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by jocdrew21 View Post
    whats the point of using them? So
    Code:
    int main(void){ }
    is fine? It will do the same thing?
    You don't need the word "void" there, but otherwise that's one of the forms that a standards-compliant (hosted) compiler must accept.

    The other form allows you to accept arguments from the command line. Try calling the following like:
    > progname hello world

    Note: argv[0] contains the program name (if available). And argv[argc] will be a NULL pointer.

    Code:
    #include <iostream>
    
    int main(int argc, char **argv) {
        for (int i = 0; i < argc; ++i)
            std::cout << argv[i] << '\n';
        return 0;
    }
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Normally, people just type void or leave what's in-between the parentheses blank.
    If you're talking about void main() then you would be incorrect. The function main must be defined to return an int (in all hosted environments). However if you're not going to use any command line arguments you should leave the argument list blank (in C you would use a void parameter).

    Code:
    int main(int argc,  char** argv) // C or C++
    or
    int main() // C++
    or 
    int main(void) // C
    As for reversing the string you could consider using string streams to process the string pushing the words onto a stack. Then clear the string and pop the words off the stack and insert them into your string.

    Jim

  8. #8
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499
    iterate it to reverse, use the stacks first in last out (FILO), then read the spaces with popping. Genius and so simple.... Thanks...

    I am going to have to google the main() question. I know it has to be a int and return an int. I have seen people use argc in there programs before but I never looked into it. I think it is about time I do.

  9. #9
    Registered User
    Join Date
    Jun 2013
    Posts
    56
    Quote Originally Posted by jocdrew21 View Post
    iterate it to reverse, use the stacks first in last out (FILO), then read the spaces with popping. Genius and so simple.... Thanks...

    I am going to have to google the main() question. I know it has to be a int and return an int. I have seen people use argc in there programs before but I never looked into it. I think it is about time I do.
    As was said it lets you take arguments into your main function and do something with them. Games use this often to set screen size etc on the fly based on a script file that the user can then change in the options menu.

    As usual when you pass an array you pass the size of the array so that the function knows how many items are in it so you dont go past the array hence the first argument is the size of the argument array while the 2nd is a pointer to the array itself.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reverse a string without string.h library
    By antros48 in forum C Programming
    Replies: 6
    Last Post: 09-10-2011, 06:01 PM
  2. reverse string
    By thescratchy in forum C Programming
    Replies: 15
    Last Post: 08-25-2010, 09:45 PM
  3. Reverse a string
    By GeorgeOfTheBush in forum C Programming
    Replies: 1
    Last Post: 12-26-2009, 01:24 PM
  4. Reverse a string (without using any string functions?)
    By geetard in forum C Programming
    Replies: 2
    Last Post: 11-15-2006, 07:42 PM
  5. Reverse A String
    By raju01 in forum C Programming
    Replies: 21
    Last Post: 01-03-2006, 01:27 PM