Author Topic: Concatenation function  (Read 7306 times)

0 Members and 1 Guest are viewing this topic.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Concatenation function
« Reply #15 on: April 02, 2021, 05:26:35 pm »
There are obviously some smart people here. OK, if you want a challenge the function (from whatever language it came, thinking VB right now but not sure) was able to take any number of strings as arguments and concatenate them, similar to our very simple tacking on of "+"'s ad infinitum. Have at it!

Code: QB64: [Select]
  1. Dim As String testarray(1 To 12)
  2.  
  3. testarray(1) = "This is"
  4. testarray(2) = " a "
  5. testarray(3) = "test"
  6. testarray(4) = " to"
  7. testarray(5) = " show"
  8. testarray(6) = " Spriggsy"
  9. testarray(7) = " had"
  10. testarray(8) = " part"
  11. testarray(9) = " of"
  12. testarray(10) = " solution"
  13. testarray(11) = " correct"
  14. testarray(12) = "."
  15.  
  16.  
  17. Print polycat$(testarray())
  18.  
  19. Function polycat$ (arr$())
  20.     c$ = arr$(LBound(arr$))
  21.     For i = LBound(arr$) + 1 To UBound(arr$)
  22.         c$ = concatenate$(c$, arr$(i))
  23.     Next
  24.     polycat$ = c$
  25.  
  26. Function concatenate$ (a$, b$)
  27.     c$ = Space$(Len(a$) + Len(b$))
  28.     Mid$(c$, 1, Len(a$)) = a$
  29.     Mid$(c$, Len(a$) + 1, Len(b$)) = b$
  30.     concatenate$ = c$
  31.  
  32.  
  33.  
« Last Edit: April 02, 2021, 06:10:12 pm by bplus »

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Concatenation function
« Reply #16 on: April 02, 2021, 06:40:37 pm »
FUNCTION Concat$(s1$, s2$)
   l = LEN(s1$) + len(s2$)
   DIM m AS _MEM: m = _MEMNEW(l)
   _MEMPUT m, m.OFFSET, s1$
   _MEMPUT m, m.OFFSET + len(s1$), s2$
   Concat$ = SPACE$(l)
   _MEMGET m, m.OFFSET, Concat$
   _MEMFREE m
END FUNCTION

If we’re looking for obscure ways to just add strings together, you can always do the above...
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: Concatenation function
« Reply #17 on: April 03, 2021, 05:13:08 am »
Hi guys and gals
@paranoia1001001
about what language has concat as native function...
I remember
Pascal both TurboPascal/ Borland Pascal both FreePascal have concat  keyword
see here
Quote
Concat : Concatenates two or more strings
Syntax : concat(s1,s2,...,sn)
Example : st:=concat(s1,s2);
If s1='ABC' and s2='DEF', st would be 'ABCDEF'
st:=concat('Borland ','Pascal ','ver. ','7.0'); Would be 'Borland Pascal ver. 7.0'
You may put as many parameters to concat as possible. If the resulting string length is more than 255, it will be truncated to 255.
Concat is the same if we use plus sign (+). For example :
st:=concat('ABC','DEF'); is the same as st:='ABC'+'DEF'
http://www.baskent.edu.tr/~tkaracay/etudio/ders/prg/pascal/PasHTM1/pas/pasl1007.html

but in the same Pascal you can use + as in BASIC
Quote
Function Concat(s1 [, s2, s3...sn] : String) : String;

Description

Concatenates 2 or more strings depending how long is the argument expression. Try to make sure not to exceed the limit of 255 characters when concatening strings as it will result in truncation. This function can also be obtained by using the plus (+) operator between strings that need to be concatenated.

Var
   S1, S2 : String;

Begin
   S1 := 'Hey!';
   S2 := ' How are you?';
   Write(Concat(S1, S2)); { 'Hey! How are you?' }
End.
is the same
Var
   S1, S2 : String;

Begin
   S1 := 'Hey!';
   S2 := ' How are you?';
   Write(S1 + S2); { 'Hey! How are you?' }
End.
https://www.pascal-programming.info/lesson10.php#jump7
https://www.freepascal.org/docs-html/rtl/system/stringfunctions.html

Thanks to read
Programming isn't difficult, only it's  consuming time and coffee

Offline xra7en

  • Seasoned Forum Regular
  • Posts: 284
    • View Profile
Re: Concatenation function
« Reply #18 on: April 03, 2021, 10:29:49 pm »
does this work for unnecessary  fun?

Code: QB64: [Select]
  1. s(1) = "I": s(2) = "love": s(3) = "qb64"
  2. 'goofy obnoxious cryptic long ver
  3. Print concat(s())
  4.  
  5.  
  6. Function concat$ (s() As String)
  7.         Dim cc As String
  8.         Dim i, j As Integer
  9.         Dim cnt As Integer
  10.         cc = "": i = 1: Do
  11.                 For j = 1 To Len(s(i)): cc = cc + Chr$(Asc(Mid$(s(i), j, 1))): Next
  12.                 cc = cc + Chr$(&H20)
  13.                 i = i + 1
  14.         Loop Until i > UBound(s)
  15.         concat$ = cc
I just like re-writing old DOS book games into modern QB64 code - weird hobby, I know!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Concatenation function
« Reply #19 on: April 04, 2021, 11:35:40 am »
Fun? or obsession? LOL

Code: QB64: [Select]
  1. Dim As String testarray(1 To 16)
  2.  
  3. testarray(1) = "Well"
  4. testarray(2) = " hell"
  5. testarray(3) = "!"
  6. testarray(4) = " I"
  7. testarray(5) = " can't"
  8. testarray(6) = " stop"
  9. testarray(7) = " trying"
  10. testarray(8) = " new"
  11. testarray(9) = " ways"
  12. testarray(10) = " to"
  13. testarray(11) = " bypass"
  14. testarray(12) = " using"
  15. testarray(13) = " +"
  16. testarray(14) = " for"
  17. testarray(15) = " concatenation"
  18. testarray(16) = "."
  19.  
  20. Print concatArr$(testarray())
  21.  
  22. Function concatArr$ (arr$())
  23.     Open "temp.txt" For Output As #1
  24.     For i = LBound(arr$) To UBound(arr$)
  25.         Print #1, arr$(i);
  26.     Next
  27.     Close #1
  28.     Open "temp.txt" For Input As #1
  29.     Line Input #1, concatArr$
  30.     Close #1
  31.     Kill "temp.txt"
  32.  
  33.  
  34.