Anyone know how to fix this lump of shit to reverse a text string? Fffuuuck
#include <iostream>
#include <string>
using namespace std;
string reverse(string& str);
void writeBackward(string s, int size)
{
if (size > 0)
{ // write the last character
cout << s.substr(size-1, 1);
// write the rest of the string backward
writeBackward(s, size-1); // Point A
} // end if
// size == 0 is the base case - do nothing
} // end writeBackward
int main()
{
string s = "Hello there";
cout << "s has " << s.length() << " characters." << endl;
string theString = "";
cout << "Enter a (backward) string: ";
getline(cin, theString);
cout << reverse(theString) << endl;
return 0;
} // end main
string reverse(string& s, int size)
{
// Implement me!!!
} // end reverse
Name:
Anonymous2009-09-29 0:58
string reverse(string& s) {
string t = "";
for (const_reverse_iterator<char> iterator = s.const_reverse_iterator.rbegin(); iterator != s.const_reverse_iterator.rend(); iterator++) {
t += *iterator;
}
s = t;
}
Simple one that only works on linked lists:
(defun my-reverse (list)
(reduce #'(lambda (a r) (cons r a)) list :initial-value nil))
Writing one that works on generic sequences is a bit longer.
reverse -> stef(;s)
;; Reverses a string in-place.
staðvær t, i, l := \lengd s, j := l
stofn
fyrir (i := 1; i < j; i := \stækka i, j := \minnka j) lykkja
t := s[i],
s[i] := s[j],
s[j] := t
lykkjulok,
s
stofnlok
}
*
"GRUNNUR"
;
It took me a while to figure out that Fjölnir strings are actually indexed from 1 to n, not 0 to n-1. Further, Fjölnir strings are prefixed with a length byte (presumably the maximum length is 255...), which, if you don't know about the 1 to n indexing, can easily be overwritten with a careless s[0] := 'a' or equivalent. Fjölnir never ceases to surprise me.