Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

I made a booboo

Name: Anonymous 2009-09-29 0:50

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: Anonymous 2009-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;
}

Name: Anonymous 2009-09-29 1:08

(reverse "string") ;=> "gnirts"

Name: Anonymous 2009-09-29 1:10

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;
      }
    return t;
  }

Name: Anonymous 2009-09-29 1:12

public String reverse(String s)
{   StringBuilder buf = new StringBuilder();
    for (int i = s.length() - 1; i >= 0; i--)
    {   buf.append(s.charAt(i));
    }
    return buf.toString();
}

Name: Anonymous 2009-09-29 1:20

Oh forgot to explain that the solution I am working towards needs to be recursive for who knows what reason.

Name: Anonymous 2009-09-29 1:27

reverse = lambda a: a[-1] + reverse(a[1:-1]) + a[0] if len(a) > 1 else a

Name: Anonymous 2009-09-29 1:29

reverse x

Name: Anonymous 2009-09-29 1:30

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.

Name: Anonymous 2009-09-29 1:34

Of course, why write one when the language provides it for you.

Name: Anonymous 2009-09-29 1:42

string reverse(string& s)
      {
        if (s.length() == 0)
            return s;
        else
          {
            return s[s.length()-1] + reverse(s.substr(0, s.length() - 1));
          }
      }

Name: Anonymous 2009-09-29 5:23

[code]'GRUNNAR'[code]

Name: Anonymous 2009-09-29 5:34

this would be so easy in C, but you had to go and use sepples.
damn, i can't even read it without my eyes vomiting (YES, MY EYES ARE VOMITING).

Name: Anonymous 2009-09-29 5:58

'GRUNNAR'

Name: GRUNNUR 2009-09-29 8:12

'GRUNNUR'

Name: meme fan 2009-09-29 8:14

'GRUNNUR'

Name: Anonymous 2009-09-29 8:48

you could, you know, just (reverse (string->list (read))) it in

Name: Anonymous 2009-09-29 8:54

>>17

you and your fictional programming language

Name: Anonymous 2009-09-29 9:14

>>18
R5RS.

Name: Anonymous 2009-09-29 11:03

>>11
Ok thats awesome, but how do I keep it from closing after execution?

Name: Anonymous 2009-09-29 13:13

>>20
Please troll elsewhere!

Name: Anonymous 2009-09-29 21:59

>>18
Lrn2pleac

Name: Anonymous 2009-09-30 1:56

>>20
Wut. It's supposed to close. It's done.

Name: Anonymous 2009-09-30 22:18



;assume ds:[bp+4] points to zero terminated string beginning
ReverseString2    proc    near
    pusha
   
    xor    cx, cx
    mov    bx. [bp+4]

    jmp    COMPARE
STORE:    push    cx
    inc    bx
COMPARE:    mov    cl, [bx]
    inc    cx
    loop    STORE

    mov    cx, bp
    sub    cx, sp
    shr    cx, 1
    mov    bx, [bp+4]
   
WRITE:    pop    [bx]   
    inc    bx
    loop    WRITE
   
    ret
ReverseString2     endp

Name: Anonymous 2009-09-30 22:21

Please disregard >>24

ReverseString2  proc    near
                pusha
   
                xor cx, cx
                mov bx. [bp+4]

                jmp COMPARE
STORE:          push    cx
                inc bx
COMPARE:        mov cl, [bx]
                inc cx
                loop    STORE

                mov cx, bp
                sub cx, sp
                shr cx, 1
                mov bx, [bp+4]
   
WRITE:          pop [bx]   
                inc bx
                loop    WRITE
   
                popa
                ret
ReverseString2  endp

Name: Anonymous 2009-09-30 22:24

fuck and [bp+4] should be mov, bx bp; add bx, 4

Name: Anonymous 2009-09-30 23:37

"reverse" < aðal ! {
aðal -> stef (;)
    stofn
        \skrifastreng \reverse "Hax my anus"
    stofnlok

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.

>>11,2,4

string reverse(const string& s) {
    return string(s.rbegin(), s.rend());
}

Name: Anonymous 2009-10-01 5:27

>>27
Pascal on beer?

Don't change these.
Name: Email:
Entire Thread Thread List