Thread: reverse the string

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    109

    reverse the string

    For my driver I have to reverse the strings in a string literal by means of a class called ReverseString. I will show you guys my function below that is supposed to reverse my string. I will also post my test for the string. My output is:
    ahsuruP
    ahsuruP
    ahsuruP

    and I am pretty sure that is not the correct output. Why doesn't my function work? Thanks for the help

    Code:
    ReverseString ReverseString::operator ~()
    {
    	int k = 0;
    	char *temp = buf;
    	for(int i = stringlength-1; i>=(stringlength/2); i--)
    	{
    		swap(temp[k], temp[i]);
    		k++;	
    	}
    	return temp;
    }
    
    
    void test19() {
    
        system("cls");
    
        cout << "19. Testing: ReverseString class." << endl << endl;
    
        csis << "19. Testing: ReverseString class." << endl << endl;
    
        ReverseString s19("Purusha");
    
        ReverseString t19;
    
        s19.setName("s19");
    
        t19.setName("t19");
    
        t19 = ~s19;
    
        s19.print();
    
        t19.print();
    
     
    
        ReverseString u19(~~s19);
    
        u19.setName("u19");
    
        u19.print();
    
        wait();
    
    }

  2. #2
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    Where is the class?
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  3. #3
    Registered User
    Join Date
    Jul 2007
    Posts
    109
    Quote Originally Posted by siavoshkc View Post
    Where is the class?
    I posted the function (~operator) but I'll post the entire header and source code:

    Code:
    //reverse.h
    
    #ifndef ReverseString_H
    #define ReverseString_H
    
    #include <iostream>
    #include "string.h"
    #include "strdrv.h"
    
    
    
    
    
    class ReverseString: public String
    {
    public:
    	ReverseString();
    	ReverseString(const ReverseString &rhs);
    	ReverseString(const char *str);
    	ReverseString operator=(const ReverseString &rhs);
    	ReverseString operator~();
    };
    
    #endif
    
    // reverse.cpp
    #define _CRT_SECURE_NO_DEPRECATE 1	
    #include <iostream>
    #include <assert.h>
    #include <algorithm>
    #include "reverse.h"
    
    
    using namespace std;
    
    ReverseString::ReverseString()
    { }
    
    ReverseString::ReverseString(const ReverseString &rhs)
    { }
    
    
    ReverseString::ReverseString(const char *str)
    {
    	name = NULL;
    	assert( str != 0);
    	stringlength = strlen(str);
    	size = stringlength + 1;
    	buf = new char[size];
    	strcpy(buf, str);
    }
    
    ReverseString ReverseString::operator =(const ReverseString &rhs)
    {
    	if(this != &rhs)
    	{
    		delete []buf;
    		stringlength = rhs.stringlength;
    		size = stringlength + 1;
    		buf = new char[size];
    		strcpy(buf, rhs.buf);
    	}
    	return *this;
    }
    
    ReverseString ReverseString::operator ~()
    {
    	int k = 0;
    	char *temp = buf;
    	for(int i = stringlength-1; i>=(stringlength/2); i--)
    	{
    		swap(temp[k], temp[i]);
    		k++;	
    	}
    	return temp;
    }

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by BKurosawa View Post
    For my driver I have to reverse the strings in a string literal by means of a class called ReverseString. I will show you guys my function below that is supposed to reverse my string. I will also post my test for the string. My output is:
    ahsuruP
    ahsuruP
    ahsuruP

    and I am pretty sure that is not the correct output. Why doesn't my function work? Thanks for the help
    So what is the output you expect?

    Your operator~ does modifiy the original string which is probably not what you wanted...

    --
    Mats

  5. #5
    Registered User
    Join Date
    Jul 2007
    Posts
    109
    Quote Originally Posted by matsp View Post
    So what is the output you expect?

    Your operator~ does modifiy the original string which is probably not what you wanted...

    --
    Mats
    I have the test up there. I might be wrong but I think that the output should be:
    Purusha
    ahsuruP
    Purusha

    How do I fix it? Thanks

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    What's the temp variable in your operator~ function? Where does it point, and how do you think you should fix it?

    --
    Mats

  7. #7
    Registered User
    Join Date
    Jul 2007
    Posts
    109
    Quote Originally Posted by matsp View Post
    What's the temp variable in your operator~ function? Where does it point, and how do you think you should fix it?

    --
    Mats
    The temp points to buf... why is that a problem?

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by BKurosawa View Post
    The temp points to buf... why is that a problem?
    In you line "t19 = ~s19;", what is buf? What gets modified in that line?

    --
    Mats

  9. #9
    Registered User
    Join Date
    Jul 2007
    Posts
    109
    Quote Originally Posted by matsp View Post
    In you line "t19 = ~s19;", what is buf? What gets modified in that line?

    --
    Mats
    What if I do this? :

    Code:
    ReverseString ReverseString::operator ~()
    {
    	int k = 0;
    	char *temp = buf;
    	for(int i = stringlength-1; i>=(stringlength/2); i--)
    	{
    		swap(temp[k], temp[i]);
    		k++;	
    	}
    	ReverseString object(temp);
    	return object;
    }

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Sorry, but did you think that through, or just randomly guess ?

    No, that's not quite right, but you have the sort of right idea. The order in which you do things is a bit wrong, that's it.

    --
    Mats

  11. #11
    Registered User
    Join Date
    Jul 2007
    Posts
    109
    >> The order in which you do things is a bit wrong.

    How so?

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Instead of asking us to solve your coding problems, how about you think about what actually happens in the code... If you UNDERSTAND what you are doing, then it's going to work so much better for you in the future. You seem to prefer the method of "randomly change the code and hope that either it fixes the problem, or someone tells you how to do it" - this is NOT the right way to learn programming.

    --
    Mats

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