Author Topic: a reverse string (Rosetta Code)  (Read 6035 times)

0 Members and 1 Guest are viewing this topic.

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
a reverse string (Rosetta Code)
« on: April 11, 2021, 05:26:27 pm »
Hi Guys and Gals of QB64 community

here a demo written for the task of rosetta code http://www.rosettacode.org/wiki/Reverse_a_string

Code: QB64: [Select]
  1. 'reverse a string
  2.  
  3. A = "Hello World!"
  4. Print A, Reverse1$(A)
  5. A = "This is QB64"
  6. Print A, Reverse2$(A)
  7. A = "QB64 is Powerful!"
  8. Print A, Reverse3$(A)
  9.  
  10. Function Reverse1$ (A As String)
  11.     Dim ReverseA As String
  12.     Dim As Integer Alenght, countFor
  13.  
  14.     Alenght = Len(A)
  15.     For countFor = 1 To Alenght Step 1
  16.         ReverseA = ReverseA + Right$(A, 1)
  17.         A = Left$(A, Alenght - countFor)
  18.     Next countFor
  19.     Reverse1$ = ReverseA
  20.  
  21. Function Reverse2$ (A As String)
  22.     Dim ReverseA As String, B As String
  23.     Dim As Integer Alenght, countFor
  24.     Alenght = Len(A)
  25.     B = A
  26.     For countFor = 1 To Alenght Step 1
  27.         B = Right$(A, countFor)
  28.         ReverseA = ReverseA + Left$(B, 1)
  29.     Next countFor
  30.     Reverse2$ = ReverseA
  31.  
  32. Function Reverse3$ (A As String)
  33.     Dim ReverseA As String
  34.     Dim As Integer Alenght, countFor
  35.  
  36.     Alenght = Len(A)
  37.     For countFor = Alenght To 1 Step -1
  38.         ReverseA = ReverseA + Mid$(A, countFor, 1)
  39.     Next countFor
  40.     Reverse3$ = ReverseA
  41.  

Please run to test o post a benchmark so we can publish this on website.
Programming isn't difficult, only it's  consuming time and coffee

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: a reverse string (Rosetta Code)
« Reply #1 on: April 11, 2021, 06:11:20 pm »
Code: QB64: [Select]
  1. s$ = "reverse"
  2. For i = Len(s$) To 1 Step -1
  3.     Print Mid$(s$, i, 1);
  4.  

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: a reverse string (Rosetta Code)
« Reply #2 on: April 11, 2021, 06:25:35 pm »
Code: QB64: [Select]
  1. r$ = "Here is a cool one!" ' b+ Recursive reverse trans QB64 2021-04-11
  2. Print reverse$(r$)
  3. Function reverse$ (s As String)
  4.     If Len(s) = 1 Then reverse$ = s Else revs$ = reverse$(Mid$(s, 2)) + Left$(s, 1)
  5.  

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: a reverse string (Rosetta Code)
« Reply #3 on: April 11, 2021, 06:33:50 pm »
Code: QB64: [Select]
  1. r$ = "Here is another one without concatenation." ' b+ reverse 2021-04-11
  2. Print reverse$(r$)
  3. Function reverse$ (s As String)
  4.     reverse$ = Space$(Len(s))
  5.     For i = Len(s) To 1 Step -1
  6.         Mid$(reverse$, Len(s) + 1 - i, 1) = Mid$(s, i, 1)
  7.     Next
  8.  

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: a reverse string (Rosetta Code)
« Reply #4 on: April 11, 2021, 06:42:42 pm »
Hi Bplus
I find cool the shortest reverse task!
I think that it is good to show different ways to get the task using different keywords, so MID$ together RIGHT$ and LEFT$ can be a good team.
Programming isn't difficult, only it's  consuming time and coffee

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: a reverse string (Rosetta Code)
« Reply #5 on: April 11, 2021, 06:54:07 pm »
Hi Bplus
I find cool the shortest reverse task!
I think that it is good to show different ways to get the task using different keywords, so MID$ together RIGHT$ and LEFT$ can be a good team.

Yes! :)
Code: QB64: [Select]
  1. r$ = "reverse"
  2. reverse r$
  3. Sub reverse (r$)
  4.     For i = Len(r$) To 1 Step -1
  5.         Locate 2, Len(r$) - i + 1: Print Chr$(Screen(1, i))
  6.     Next
  7.  

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: a reverse string (Rosetta Code)
« Reply #6 on: April 11, 2021, 06:57:02 pm »
Oh this is better:
Code: QB64: [Select]
  1. r$ = "This is better."
  2. reverse r$
  3. Sub reverse (r$)
  4.     For i = Len(r$) To 1 Step -1
  5.         Locate 1, Len(r$) + 1 + Len(r$) - i + 1: Print Chr$(Screen(1, i))
  6.     Next

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: a reverse string (Rosetta Code)
« Reply #7 on: April 11, 2021, 07:09:30 pm »
Here is a fun one!

Code: QB64: [Select]
  1. _Title "Please enter a string to reverse..."
  2. While _KeyDown(27) = 0 And _KeyDown(13) = 0
  3.     k$ = InKey$
  4.     If Len(k$) Then
  5.         If Asc(k$) = 8 Then temp$ = Mid$(temp$, 2) Else temp$ = k$ + temp$
  6.         Cls
  7.         Print temp$
  8.     End If
  9.     _Limit 10
  10.  

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: a reverse string (Rosetta Code)
« Reply #8 on: April 11, 2021, 07:40:18 pm »
This one is fun to watch!
Code: QB64: [Select]
  1. r$ = "0123456789"
  2. reverse r$
  3. Sub reverse (r$)
  4.     Print r$
  5.     k = 2
  6.     While k <= Len(r$)
  7.         For i = Len(r$) To k Step -1
  8.             t$ = Mid$(r$, i, 1)
  9.             Mid$(r$, i, 1) = Mid$(r$, i - 1, 1)
  10.             Mid$(r$, i - 1, 1) = t$
  11.             Locate 2, 1: Print r$
  12.             _Limit 1
  13.         Next
  14.         k = k + 1
  15.     Wend
  16.  

Offline euklides

  • Forum Regular
  • Posts: 128
    • View Profile
Re: a reverse string (Rosetta Code)
« Reply #9 on: April 12, 2021, 03:22:16 am »
a$ = "reverse": b$ = "": FOR x = 1 TO LEN(a$): b$ = MID$(a$, x, 1) + b$: NEXT x: PRINT b$: SLEEP 
Why not yes ?

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: a reverse string (Rosetta Code)
« Reply #10 on: April 13, 2021, 11:03:41 am »
Here is my version of this challenge, using @TempodiBasic 's suggestion of functions and subs having the same name:

Code: QB64: [Select]
  1.  
  2.  
  3. test = "This is a test!"
  4.  
  5. Print Reverse(test)
  6. Print test
  7. Reverse test
  8. Print test
  9.  
  10. Function Reverse$ (rev As String)
  11.     Dim As String s: s = rev
  12.     Dim As String * 1 sArr(0 To Len(s))
  13.     n = Len(s) + 1
  14.     For i = LBound(sArr) To UBound(sArr)
  15.         sArr(i) = Mid$(s, i, 1)
  16.     Next
  17.     For i = 0 To (n / 2) - 1
  18.         Swap sArr(i), sArr(n - i - 1)
  19.     Next
  20.     s = ""
  21.     For i = LBound(sArr) To UBound(sArr) - 1
  22.         s = s + sArr(i)
  23.     Next
  24.     Reverse = s
  25.  
  26. Sub Reverse (s As String)
  27.     Dim As String * 1 sArr(0 To Len(s))
  28.     n = Len(s) + 1
  29.     For i = LBound(sArr) To UBound(sArr)
  30.         sArr(i) = Mid$(s, i, 1)
  31.     Next
  32.     For i = 0 To (n / 2) - 1
  33.         Swap sArr(i), sArr(n - i - 1)
  34.     Next
  35.     s = ""
  36.     For i = LBound(sArr) To UBound(sArr) - 1
  37.         s = s + sArr(i)
  38.     Next

 
Screenshot 2021-04-13 110329.png
« Last Edit: April 13, 2021, 11:23:02 am by SpriggsySpriggs »
Shuwatch!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: a reverse string (Rosetta Code)
« Reply #11 on: April 13, 2021, 12:57:17 pm »
Oh here is my best yet!
Code: QB64: [Select]
  1. _Title "Reverse Everything!" 'b+ 2021-04-13
  2.  
  3. Screen _NewImage(216, 64, 32)
  4. _Delay .25 'need time for screen to load before attempting to move it.
  5.  
  6. Locate 2, 11: Print "reverse"
  7. _PutImage (0, 32), 0, 0, (214, 16)-Step(-216, 16)
  8.  

 
reverse.PNG


Edit: seemed a pixel off in first screen shot, so I moved over.
« Last Edit: April 13, 2021, 01:06:12 pm by bplus »

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: a reverse string (Rosetta Code)
« Reply #12 on: April 13, 2021, 03:11:50 pm »
And another version. I know, not really QB64.

Code: C++: [Select]
  1. //save this as reverse.h
  2. #include <bits/stdc++.h>
  3. const char* ReverseString(const char* str){
  4.         std::string str2 = str;
  5.         reverse(str2.begin(),str2.end());
  6.         return str2.c_str();
  7. }

Code: QB64: [Select]
  1.  
  2.     Function Reverse$ Alias "ReverseString" (str As String)
  3.  
  4. Print Reverse("This is a test")
Shuwatch!

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: a reverse string (Rosetta Code)
« Reply #13 on: April 13, 2021, 05:44:16 pm »
Hey boys
the challange on new ways to do a reverse string is being interesting!
Programming isn't difficult, only it's  consuming time and coffee

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: a reverse string (Rosetta Code)
« Reply #14 on: April 13, 2021, 06:46:27 pm »
And another one!
Code: QB64: [Select]
  1. _Title "Swinging Reverse" 'b+ 2021-04-13
  2.  
  3. Screen _NewImage(216, 64, 32)
  4. _Delay .25 'need time for screen to load before attempting to move it.
  5. xc = _Width / 2: yc = _Height / 2
  6. _PrintString (xc - 7 * 4, yc - 8), "reverse"
  7. _PutImage , 0, r&
  8. xs = 4: dx = -.1
  9.     Cls
  10.     RotoZoom3 xc, yc, r&, xs, 4, 0
  11.     xs = xs + dx
  12.     If xs < -4 Then xs = -4: dx = -dx
  13.     If xs > 4 Then xs = 4: dx = -dx
  14.     _Display
  15.     _Limit 30
  16.  
  17. Sub RotoZoom3 (X As Long, Y As Long, Image As Long, xScale As Single, yScale As Single, radianRotation As Single) ' 0 at end means no scaling of x or y
  18.     Dim px(3) As Single: Dim py(3) As Single
  19.     Dim W&, H&, sinr!, cosr!, i&, x2&, y2&
  20.     W& = _Width(Image&): H& = _Height(Image&)
  21.     px(0) = -W& / 2: py(0) = -H& / 2: px(1) = -W& / 2: py(1) = H& / 2
  22.     px(2) = W& / 2: py(2) = H& / 2: px(3) = W& / 2: py(3) = -H& / 2
  23.     sinr! = Sin(-radianRotation): cosr! = Cos(-radianRotation)
  24.     For i& = 0 To 3
  25.         x2& = xScale * (px(i&) * cosr! + sinr! * py(i&)) + X: y2& = yScale * (py(i&) * cosr! - px(i&) * sinr!) + Y
  26.         px(i&) = x2&: py(i&) = y2&
  27.     Next
  28.     _MapTriangle _Seamless(0, 0)-(0, H& - 1)-(W& - 1, H& - 1), Image To(px(0), py(0))-(px(1), py(1))-(px(2), py(2))
  29.     _MapTriangle _Seamless(0, 0)-(W& - 1, 0)-(W& - 1, H& - 1), Image To(px(0), py(0))-(px(3), py(3))-(px(2), py(2))
  30.  
  31.