Thread: Help! I need to reverse a string!!

  1. #16
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    int x;
    int length = strlen(orignal_string);

    //length will be from 0 to 20 depending on the string actually in original_string. DON'T use 21, which is the maximal possible number of char in original_string, but may not be the actual number of char in original_string.

    for (x = 0; x < length; x++)
    {
    reversed_string[x] = original_string[length - x
    ];
    }

    //now reversed_string is a non-null terminated char array with lenght number of char in it. You need to add a null char at the end of the real char in reversed_string to change reversed_string into a string. The value of x == length at this point and represents where the null char should go.

    reversed_string[x] = '\0';



  2. #17
    Registered User
    Join Date
    Dec 2004
    Posts
    13
    Here's what I have now:

    Code:
    // PROJECT 10-1.CPP
    
    #include <iostream>
    #include <cstring>
    using namespace std;
    
    char original_string, reversed_string;
    int x;
    int length = strlen(original_string);
    
    char reverse_string(char original_string)
    {
        for (x = 0; x < length; x++)
        {
            reversed_string[x] = original_string[length - x];
        }
        reversed_string[x] = '\0';
        return reversed_string[x];
    }
    
    int main()
    {
        cout << "Input a string to be reversed: ";
        cin.get(original_string, size);
        cout << '\n';
        reversed_string = reverse_string(original_string);
        cout << reversed_string[x] << '\n';
        return 0;
    }
    Please, can somebody just copy my code, fix it, and post it? I'm still stuck...Thank you, though, again.
    Last edited by U79; 12-22-2004 at 05:08 PM.

  3. #18
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>Please, can somebody just copy my code, fix it, and post it?
    That would be against the rules, I believe But we can still help out.

    >>int length = strlen(original_string);
    This should be inside the reverse_string function, since length isn't needed anywhere else (and besides, at that point in the program original_string doesn't contain anything, so you'll get a catastrophic error).

    >>int x;
    The same goes for this, though you won't get any runtime errors. Fix it anyways.

    >>cout << reversed_string[x] << '\n';
    You want to print reversed_string (a string), not reversed_string[x] (a character).

    >>char reverse_string(char original_string)
    reverse_string should take a char* and return a char*, not char. char is just one character; char* can be a string (array of characters).

    >>reversed_string = reverse_string(original_string);
    You can't copy strings like that. Since reversed_string is a global variable, and it is modified directly inside reverse_string(), you don't need to say reversed_string = ....

    >>char original_string, reversed_string;
    Since you want original_string and reversed_string to hold strings, this should be:
    char original_string[500], reversed_string[500];

    And accordingly size in
    >>cin.get(original_string, size);
    will be 500.

    >>return reversed_string[x];
    Again, you want to return reversed_string.

    Code:
    for (x = 0; x < length; x++)
    {
        reversed_string[x] = original_string[length - x];
    }
    Two problems with this. First, you need to swap the characters, not just copy one over. Here's an example of how you can do it:
    Code:
    char temp = reversed_string[x];  //Store the value of the current character
    reversed_string[x] = original_string[length - x];  //Overwrite the current character from somewhere else
    original_string[length - x] = temp;  //Copy the original value into the other place
    The second problem with this is, once you reach the halfway point, your code just keeps going and reverses the whole string back to where it was. So you need to change the loop condition to x < (length / 2). That way you don't go past halfway.

    Just as a note, all your problems with "return reversed_string[x];" come from you typing random things hoping that they will make compiler errors go away. WRONG THING TO DO! You'll just shoot yourself in the foot like you did now. If you can't figure out why the error's there, ask somebody. As a rule, don't do something unless you know that what you're doing is going to fix the REAL problem.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  4. #19
    Registered User
    Join Date
    Dec 2004
    Posts
    13
    >>cout << reversed_string[x] << '\n';
    You want to print reversed_string (a string), not reversed_string[x] (a character).
    How exactly do I do that?

    Code:
    // PROJECT 10-1.CPP
    
    #include <iostream>
    #include <cstring>
    using namespace std;
    
    char original_string[500], reversed_string[500];
    const int size = 500;
    int x;
    
    char* reverse_string(char* original_string)
    {
        int length = strlen(original_string);
        for (x = 0; x < (length / 2); x++)
        {
            char temp = reversed_string[x];
            reversed_string[x] = original_string[length - x];
            original_string[length - x] = temp;
        }
        reversed_string[x] = '\0';
        return reversed_string;
    }
    
    int main()
    {
        cout << "Input a string to be reversed: ";
        cin.get(original_string, size);
        reverse_string(original_string);
        cout << reversed_string[x] << '\n';
        system ("pause");
        return 0;
    }
    Thank you again very much!
    Last edited by U79; 12-22-2004 at 05:38 PM.

  5. #20
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Exactly what it looks like.
    cout << reversed_string;
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  6. #21
    Registered User
    Join Date
    Dec 2004
    Posts
    13
    It's still not working like this:

    Code:
    // PROJECT 10-1.CPP
    
    #include <iostream>
    #include <cstring>
    using namespace std;
    
    const int size = 500;
    char original_string[size], reversed_string[size];
    int x;
    
    char* reverse_string(char* original_string)
    {
        int length = strlen(original_string);
        for (x = 0; x < (length / 2); x++)
        {
            char temp = reversed_string[x];
            reversed_string[x] = original_string[length - x];
            original_string[length - x] = temp;
        }
        reversed_string[x] = '\0';
        return reversed_string;
    }
    
    int main()
    {
        cout << "Input a string to be reversed: ";
        cin.get(original_string, size);
        reverse_string(original_string);
        cout << reversed_string << '\n';
        return 0;
    }
    Again, thank you.
    Last edited by U79; 12-23-2004 at 07:40 AM.

  7. #22
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Code:
    char* reverse_string(char* original_string)
    {    
        int x, y;
        char temp;
        for (x = 0,  y = strlen(original_string) - 1; x < y; --y, x++)
        {
          temp = original_string[x];
          original_string[x] = original_string[y];
          original_string[y] = temp;
        }
        return original_string;
    }
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  8. #23
    Registered User
    Join Date
    Dec 2004
    Posts
    13
    Sweet, sweet working program. Thank you again so unbelievably much, everybody! This is the code that ended up working (now I just have to add coments! ):

    Code:
    // PROJECT 10-1.CPP
    
    #include <iostream>
    #include <cstring>
    using namespace std;
    
    const int size = 500;
    char original_string[size], reversed_string[size];
    
    char* reverse_string(char* original_string)
    {    
        int x, y;	
        char temp;
        for (x = 0, y = strlen(original_string) - 1; x < y; --y, x++)
        {						
          temp = original_string[x];
          original_string[x] = original_string[y];
          original_string[y] = temp;
        }						
        return original_string;
    }
    
    int main()
    {
        cout << "Input a string to be reversed: ";
        cin.get(original_string, size);
        cout << "Reversed: ";
        reverse_string(original_string);
        cout << original_string << '\n';
        return 0;
    }
    Thank you again for helping me all this time until the program worked!
    Last edited by U79; 12-23-2004 at 09:51 AM.

  9. #24
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    If you want to swap the contents of reverse_string and original_string, fine. If you want to reverse original_string using the same memory by swapping char, fine. But if you don't want to change original_string and all you want to know is what the reverse of original_string is then just copy every char in original_string into reverse_string from back to front and print out reverse_string.

    const int MAX = 12;
    char original_string[MAX] = "and";
    char reverse_string[MAX];

    void reverseString(const char * original_string, char * reverse_string)
    {
    int x;
    int length = strlen(original_string);
    for(x = 0; x < length; ++x)
    reverse_string[x] = original_string[length - 1 - x];
    reverse_string[x] = '\0';
    }

    Note the change to my original version in that when x == 0, original_string[length - x] equates to original_string[length] which is the null char. You don't want to copy the null char to reverse_string[0]. You want to copy original_string[length - 1] to reverse_string[0] and original_string[length - 2] to reverse_string[1], etc. In general, you want to copy original_string[(length - 1) - x] to reverse_string[x]. You could swap original_string[length - 1 - x] with original_string[x] for each x < length/2, too, if you want to swap elements in original_string without using a separate string to hold the reversed string.
    Last edited by elad; 12-23-2004 at 11:37 AM.

  10. #25
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Here is how to reverse a string using the STL:
    Code:
    #include <iostream>
    #include <algorithm>
    #include <string>
    
    int main()
    {
        std::string myString = "Another string to reverse";
        std::copy(myString.rbegin(),myString.rend(), std::ostream_iterator<char>(std::cout) );
    }
    EDIT: Or if you want to store the result:
    Code:
        string myString = "Petter Strandmark";
        string reString(myString.length(),' ');
    
        copy(myString.rbegin(),myString.rend(), reString.begin() );
    Last edited by Sang-drax; 12-23-2004 at 11:29 AM.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  11. #26
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817

    Red face

    Quote Originally Posted by Sang-drax
    Here is how to reverse a string using the STL:
    Or...

    Code:
    #include <string>
    #include <iterator>
    #include <algorithm>
    
    int main()
    {
        std::string str = "Another string to reverse";
        std::reverse_copy(str.begin(),str.end(), std::ostream_iterator<char>(std::cout));
    }
    Quote Originally Posted by Sang-drax
    Or if you want to store the result:
    Or...

    Code:
    std::string str = "Petter Strandmark";
    std::reverse(str.begin(),str.end());
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  12. #27
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> std::string str = "Petter Strandmark";

    now what's yours?

    and also:

    Code:
    char data[] = {"fumanchu"};
    std::reverse(data, data + std::strlen(data));
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  13. #28
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    Read the comments
    PHP Code:
    // PROJECT 10-1.CPP

    #include <iostream>
    using namespace std;

    const 
    int size 21;
    char original_string[size], reversed_string[size];
    int x;

    //this function returns a single character
    char reverse_string(char original_string)
    {              
    //why are you using x < 21, the user can enter less than 21 characters. lookup a function called strlen() it will do the job.
        
    for (021x++)
        {
            
    reversed_string[x] = original_string[21-x];
        }
        return 
    reversed_string[x]; //you are returning the character at position 21 ( which is not even in your string, because you only allocated 21 char's 0 - 20 )... I don't think that's what you want
    }

    int main()
    {
        
    cout << "Input a string to be reversed: ";
        
    cin.get(original_stringsize);
        
    cout << '\n';
                    
    //reversed_string is an array, reverse_string returns a char
        
    reversed_string reverse_string(original_string);
                    
    //you are displaying the character at position 21 which is also not a part of your string, for the same reason mentioned above.
        
    cout << reversed_string[x] << '\n';
        return 
    0;

    Try fixing these issues, and come back with that you have, we will be happy to help...
    Last edited by ammar; 12-24-2004 at 07:15 AM.
    none...

  14. #29
    Registered User
    Join Date
    Feb 2002
    Posts
    13
    You wasted alot of code there LOL

    Code:
    #include <iostream>
    #include <cstring>
    using namespace std;
    
    const int size = 500;
    char original_string[size], reversed_string[size];
    
    char* reverse_string(char* original_string)
    {    
        int x, y =  strlen(original_string);
        for (x = 0; x < strlen(original_string); x++)
        {	
    		
          reversed_string[--y] = original_string[x];
    	  
        }	
        return reversed_string;
    }
    
    int main()
    {
        cout << "Input a string to be reversed: ";
        cin.get(original_string, size);
        cout << "Reversed: ";
        reverse_string(original_string);
        cout << reversed_string << '\n';
        return 0;
    }
    Last edited by Shahram_z; 12-24-2004 at 04:32 PM.
    Ok...

  15. #30
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by U79
    Sweet, sweet working program. Thank you again so unbelievably much, everybody! This is the code that ended up working (now I just have to add coments! ):
    Code:
    char* reverse_string(char* original_string)
    {    
        int x, y;	
        char temp;
        for (x = 0, y = strlen(original_string) - 1; x < y; --y, x++)
        {						
          temp = original_string[x];
          original_string[x] = original_string[y];
          original_string[y] = temp;
        }						
        return original_string;
    }
    Thank you again for helping me all this time until the program worked!
    So in reality all you really did is a copy-paste of Sebastiani's code. Good job. I'm sure you'll do wonderfully on your tests. I hope you fail your class.

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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. ........ed off at functions
    By Klinerr1 in forum C++ Programming
    Replies: 8
    Last Post: 07-29-2002, 09:37 PM