toupper, which I think is in <cctype>. It converts one character at a time.
toupper, which I think is in <cctype>. It converts one character at a time.
I think the unary + shouldn't do anything. At least, that's what it does for numeric types.
Unary - might return a reversed string?
Last edited by anon; 08-07-2007 at 03:13 PM.
I might be wrong.
Quoted more than 1000 times (I hope).Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
Thanks everybody for your help so far. I am pretty close to finishing, but the problem right now is that my reverse string function (shown below) isn't reversing the string. It is only outputting the string multiple times. I do not know why. I have supplied the reverse string function and the test from the driver that utilizes it.
Code:ReverseString ReverseString::operator ~() { char *temp = buf; for(int i = stringlength; i>= 0; i--) { swap(temp[stringlength - i], temp[i]); } 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(); }
[QUOTE=BKurosawa;662796]
You should only swap half the string (because once you are past the middle, you are swapping it back again - think about it...)Code:ReverseString ReverseString::operator ~() { char *temp = buf; for(int i = stringlength; i>= 0; i--) { swap(temp[stringlength - i], temp[i]); } return temp; }
Also, if your swap works as a proper swap, you don't need to swap it "from the back", do you?
--
Mats
buf[stringlength] = '\0'
you want to leave it as a last char - not to move it in he first cahr effectively truncating the string
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler
I still don't have it. What is wrong with this? Thanks for the help BTW.
Code:ReverseString ReverseString::operator ~() { int k = 0; char *temp = new char[size]; temp = buf; for(int i = stringlength-1; i>=(stringlength/2); i--) { k++; swap(temp[k], temp[i]); } temp[stringlength] = '\0'; return temp; }
Not having taken the time to reread the previous posts, I am wondering whether you defined the conversion of char* to ReverseString.
And also... what does this do?
What happens to the memory you allocated?Code:temp = buf;
"If you tell the truth, you don't have to remember anything"
-Mark Twain
what is wrong with this code?
Thanks everybody!Code:ReverseString ReverseString::operator ~() { int k = 0; char *temp = new char[size]; for(int i = stringlength-1; i>=(stringlength/2); i--) { k++; swap(temp[k], temp[i]); } temp[stringlength] = '\0'; return temp; }
Did you forget something?
Luckily, that isn't necessary. See if you understand this:
Code:char* a = temp; char* b = orig + size; int count = size; while(count--) *a++ = *(--b); temp[size] = '\0';
Last edited by CodeMonkey; 08-09-2007 at 01:04 AM. Reason: Vart was right
"If you tell the truth, you don't have to remember anything"
-Mark Twain
you increase k before swapping
so temp[0] is leaved uninitialized
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler