6-line procedure to convert to mixed case

Share your advanced PureBasic knowledge/code with the community.
remi_meier
Enthusiast
Enthusiast
Posts: 468
Joined: Sat Dec 20, 2003 6:19 pm
Location: Switzerland

Post by remi_meier »

So then...

Code: Select all

Procedure Test(HoldString.l, AtPosition.l = 0, Delimiter.c = ' ') 
  While PeekC(HoldString + (AtPosition << #PB_Compiler_Unicode)) + 0 * (PokeC(HoldString + (AtPosition << #PB_Compiler_Unicode), Asc(LCase(Chr(PeekC(HoldString + (AtPosition << #PB_Compiler_Unicode))))) - (32 * ((AtPosition = 0) Or (PeekC(HoldString + ((AtPosition - 1) << #PB_Compiler_Unicode)) = Delimiter)))) ) + 0 * PokeL(@AtPosition, PeekL(@AtPosition) + 1)
  Wend 
EndProcedure
Athlon64 3700+, 1024MB Ram, Radeon X1600
User avatar
Flype
Addict
Addict
Posts: 1542
Joined: Tue Jul 22, 2003 5:02 pm
Location: In a long distant galaxy

Post by Flype »

so if speed isn't problem
and cheating isn't allowed,
i have also a 6-Lines code in the course :

Code: Select all

Procedure.s Capitalize1(string.s, sep.s = " ")
  Protected word.s = StringField(string, 1, sep)
  If word 
    ProcedureReturn UCase(Left(word, 1)) + LCase(Right(word, Len(word) - 1)) + sep + Capitalize1(Right(string, Len(string) - Len(word) - 1), sep)
  EndIf
EndProcedure
it's a pity that purebasic do not have the 'If' / 'Then' statement because i could save 2 more lines :D
Last edited by Flype on Tue Nov 07, 2006 10:01 pm, edited 1 time in total.
No programming language is perfect. There is not even a single best language.
There are only languages well suited or perhaps poorly suited for particular purposes. Herbert Mayer
User avatar
Flype
Addict
Addict
Posts: 1542
Joined: Tue Jul 22, 2003 5:02 pm
Location: In a long distant galaxy

Post by Flype »

@remi_meier
oups - didn't seen your post !
4-Lines :shock: excellent trick.
No programming language is perfect. There is not even a single best language.
There are only languages well suited or perhaps poorly suited for particular purposes. Herbert Mayer
Xombie
Addict
Addict
Posts: 898
Joined: Thu Jul 01, 2004 2:51 am
Location: Tacoma, WA
Contact:

Post by Xombie »

remi_meier wrote:So then...

Code: Select all

Procedure Test(HoldString.l, AtPosition.l = 0, Delimiter.c = ' ') 
  While PeekC(HoldString + (AtPosition << #PB_Compiler_Unicode)) + 0 * (PokeC(HoldString + (AtPosition << #PB_Compiler_Unicode), Asc(LCase(Chr(PeekC(HoldString + (AtPosition << #PB_Compiler_Unicode))))) - (32 * ((AtPosition = 0) Or (PeekC(HoldString + ((AtPosition - 1) << #PB_Compiler_Unicode)) = Delimiter)))) ) + 0 * PokeL(@AtPosition, PeekL(@AtPosition) + 1)
  Wend 
EndProcedure
Ahahaha! That's the ugliest piece of code I've seen in a long time :) Good job, remi_meier, my hat is off to you :)
Trond wrote:Hey changing the parameter type of the string is cheating!
I don't know what you're talking about :o I'm just... saving the original programmer's time is all >_>
remi_meier
Enthusiast
Enthusiast
Posts: 468
Joined: Sat Dec 20, 2003 6:19 pm
Location: Switzerland

Post by remi_meier »

Oh there are some more ugliest codes :P

va!n + me

Code: Select all

Prototype glOrtho(a.d,b.d,c.d,d.d,e.d,f.d) : Prototype gluPerspective(a.d,b.d,c.d,d.d)
Global glOrtho_.glOrtho = 0*OpenLibrary(0, "OpenGL32.dll") + GetFunction(0, "glOrtho"), gluPerspective_.gluPerspective = 0*OpenLibrary(1, "glu32.dll") + GetFunction(1, "gluPerspective"), StartTime.d = GetTickCount_(), pfd.PIXELFORMATDESCRIPTOR\dwFlags = #PFD_DOUBLEBUFFER | #PFD_SUPPORT_OPENGL, hdc = GetDC_ (CreateWindow_("edit", 0, #WS_POPUP|#WS_VISIBLE|#WS_MAXIMIZE,0, 0, 0 , 0, 0, 0, 0, 0) )  : t=SetPixelFormat_ (hdc, ChoosePixelFormat_ ( hdc, pfd) , pfd ) + wglMakeCurrent_ (hdc, wglCreateContext_(hdc) ) + ShowCursor_(#False) + InitSound() + glClearColor_(0.4,0.0,0.6,0.0) + glViewport_(0.0,0.0,GetSystemMetrics_(0),GetSystemMetrics_(1)) + glMatrixMode_($1701) + glLoadIdentity_() + gluPerspective_(45.0,800.0/600.0,1.0,500.0) + glMatrixMode_($1700) + glEnable_($0B44) + glEnable_($0B71) + glDepthMask_(1) + glDisable_($0B44)
Procedure OGLRect(x1.f, y1.f, x2.f, y2.f, r.f, g.f, b.f) : t = glMatrixMode_($1701) + glLoadIdentity_() + glOrtho_(0,1024, 0,768,-2048, 2048) + glMatrixMode_($1700) + glLoadIdentity_() + glColor3f_(r, g, b) + glRectf_(x1, y1, x2, y2) + glMatrixMode_($1701) + glLoadIdentity_() + gluPerspective_(45.0,800.0/600.0,1.0,500.0) + glMatrixMode_($1700)
EndProcedure : Procedure CreateCube( x.d, y.d, z.d)
  Protected dRotate.d = (GetTickCount_() - StartTime) / 10, t = glLoadIdentity_() + glTranslatef_(x.d,y.d,z.d) + glRotatef_(dRotate, dRotate,dRotate, 0), *datad.Double = ?dat_cube : For loop = 0 To 1 + (0 * ((loop < 2) And glBegin_(Int(PeekD(*datad))))) 
  For i = 0 To (Int(PeekD(*datad+8))-1) + 0 * (((Not (Abs(PeekD(*datad+16+32*i)-1) < 0.01)) + (Not (glColor3dv_(*datad+24+32*i)))) And glVertex3dv_(*datad+24+32*i))  :  Next 
*datad + (16 + Int(PeekD(*datad+8)) * 32) + 0 * (glEnd_()) : Next 
EndProcedure : Procedure DrawCube(n.l, *p.Double)
ProcedureReturn (CreateCube(PeekD(*p)+6, PeekD(*p+8)+2.5, -45 + Sin((GetTickCount_()-StartTime)/666)*10 )) + (n > 1 And DrawCube(n-1, *p + 16)) : EndProcedure
Repeat : Until (0 * ((0* (glClear_($00004000 | $00000100) + DrawCube(42, ?dat_cube + $3A0) + OGLRect(0, 0, 1024, 100, 0, 0, 0) + OGLRect(0, 668, 1024, 768, 0, 0, 0) + Sleep_(10) + SwapBuffers_ ( hdc ) )))) Or ((GetAsyncKeyState_(#VK_ESCAPE) And ShowCursor_(#True)) And TerminateProcess_(GetCurrentProcess_(), 0))
dat_cube: ! dq   7.0, 10.0, 1.0, 1.0, 0.0, 0.0, 2.0, -1.0, 1.0, 1.0, 2.0, 1.0, 1.0, 1.0, 2.0, 1.0, 1.0, -1.0, 2.0, -1.0, 1.0, -1.0, 1.0, 0.0, 0.0, 1.0, 2.0, -1.0, -1.0, 1.0, 2.0, -1.0, -1.0, -1.0, 2.0, 1.0, -1.0, -1.0, 2.0, 1.0, -1.0, 1.0, 5.0, 18.0, 1.0, 1.0, 1.0, 1.0, 2.0, -1.0, 1.0, 1.0, 2.0, -1.0, -1.0, 1.0, 2.0, 1.0, 1.0, 1.0, 1.0, 0.0, 1.0, 0.0, 2.0, 1.0, -1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 2.0, 1.0, 1.0, -1.0, 1.0, 0.0, 1.0, 1.0, 2.0, 1.0, -1.0, -1.0, 1.0, 0.6, 0.6, 0.6, 2.0, -1.0, 1.0, -1.0, 1.0, 0.1, 0.1, 0.1, 2.0, -1.0, -1.0, -1.0, 1.0, 0.0, 0.0, 1.0, 2.0, -1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 1.0, 2.0, -1.0, -1.0, 1.0, -22.5, 0.0, -20.5, 2.0,-18.5, 2.0, -16.5, 2.0, -14.5, 0.0, -18.5, -2.0, -16.5, -2.0, -20.5, -4.0, -22.5, -6.0, -20.5, -6.0, -18.5, -6.0, -16.5, -6.0, -14.5, -6.0, -10.5, 2.0, -8.5, 2.0, -6.5, 2.0, -4.5, 2.0, -2.5, 2.0, -10.5 ,0.0, -10.5, -2.0,-8.5, -2.0, -6.5, -2.0, -4.5, -2.0, -2.5, -4.0, -10.5, -6.0, -8.5, -6.0, -6.5, -6.0, -4.5, -6.0, 4.5, 2.0, 6.5, 2.0, 8.5, 2.0, 10.5, 2.0, 2.5, 0.0, 2.5, -2.0, 4.5, -2.0, 6.5, -2.0, 8.5, -2.0, 2.5, -4.0, 10.5, -4.0,  4.5, -6.0,  6.5, -6.0, 8.5, -6.0
or also by va!n and me:

Code: Select all

InitSprite() Xor OpenScreen(640,480,32,"evolution - 256 lines of code by va!n and remi_meier") Xor StartDrawing(ScreenOutput()) : Repeat
  temp = (temp + 2 * Val(Str(z = 639 * 339 Or 0))) % 256 : z = (z + 1) % (639 * 339 + 1)
Until ((Plot (z%639, (z/639)+70, RGB( Sin(Val(Str(z%639))*(2*#PI/360))*(z/639)+temp, Cos(Val(Str(z%639))*(2*#PI/360))*(z/639)-temp, 240))=7) Xor (z = 639 * 339 And (StopDrawing() Xor FlipBuffers() Xor StartDrawing(ScreenOutput()))) ) Or (GetAsyncKeyState_(#VK_ESCAPE))
for a contest where one : per line was allowed.


But who needs such code anyway :lol:
Athlon64 3700+, 1024MB Ram, Radeon X1600
Xombie
Addict
Addict
Posts: 898
Joined: Thu Jul 01, 2004 2:51 am
Location: Tacoma, WA
Contact:

Post by Xombie »

Well, I hadn't thought calling an unrelated function in an equation like...

Code: Select all

debug a + (0 * PokeL(@a, 20))
... that. Reminds me a little of the whole C++ thing var++ and ++var
User avatar
Flype
Addict
Addict
Posts: 1542
Joined: Tue Jul 22, 2003 5:02 pm
Location: In a long distant galaxy

Post by Flype »

just to be curious, here is a little speed test

results ( with some modifications in your codes to fit in the challenge ) :

RemiMeier: 4 lines, 220ms
Flype2: 5 lines, 670ms
Xombie: 6 lines, 205ms
TheMexican: 6 lines, 720ms
Trond: 7 lines, 2453ms
Flype1: 17 lines, 62ms

Code: Select all

;-

Procedure Capitalize_RemiMeier(HoldString.l, AtPosition.l = 0, Delimiter.c = ' ') ; 4 lines
  While PeekC(HoldString + (AtPosition << #PB_Compiler_Unicode)) + 0 * (PokeC(HoldString + (AtPosition << #PB_Compiler_Unicode), Asc(LCase(Chr(PeekC(HoldString + (AtPosition << #PB_Compiler_Unicode))))) - (32 * ((AtPosition = 0) Or (PeekC(HoldString + ((AtPosition - 1) << #PB_Compiler_Unicode)) = Delimiter)))) ) + 0 * PokeL(@AtPosition, PeekL(@AtPosition) + 1)
  Wend
EndProcedure

Procedure.s Capitalize_Flype2(string.s, separator.s = " ") ; 5 lines
  If StringField(string, 1, separator)
    ProcedureReturn UCase(Left(StringField(string, 1, separator), 1)) + LCase(Right(StringField(string, 1, separator), Len(StringField(string, 1, separator)) - 1)) + separator + Capitalize_Flype2(Right(string, Len(string) - Len(StringField(string, 1, separator)) - 1), separator)
  EndIf
EndProcedure 

Procedure Capitalize_Xombie(HoldString.l, AtPosition.l = 0, Delimiter.c = ' ') ; 6 lines
  While PeekC(HoldString + (AtPosition << #PB_Compiler_Unicode))
    PokeC(HoldString + (AtPosition << #PB_Compiler_Unicode), Asc(LCase(Chr(PeekC(HoldString + (AtPosition << #PB_Compiler_Unicode))))) - (32 * ((AtPosition = 0) Or (PeekC(HoldString + ((AtPosition - 1) << #PB_Compiler_Unicode)) = Delimiter))))
    AtPosition + 1
  Wend
EndProcedure

Procedure.s Capitalize_TheMexican(string.s, separator.s = " ") ; 6 lines
  For i = 1 To CountString(string, separator) + 1
    mixed.s + UCase(Left(LCase(StringField(string, i, separator)), 1)) + Right(LCase(StringField(string, i, separator)), Len(LCase(StringField(string, i, separator))) - 1) + separator
  Next
  ProcedureReturn Trim(mixed)
EndProcedure 

Procedure.s Capitalize_Trond(string.s, separator.s = " ") ; 7 lines
  string = separator + LCase(string)
  For i = 0 To 25
    ReplaceString(string, separator + Chr('a' + i), separator + Chr('A' + i), 2)
  Next
  ProcedureReturn Right(string, Len(string) - 1)
EndProcedure

Procedure.s Capitalize_Flype1(string.s, separator.c = ' ') ; 17 lines
  Protected last.c = separator, *s.Character = @string 
  While *s\c 
    If last = separator
      If *s\c >= 'a' And *s\c <= 'z' : *s\c - 32 : EndIf
    Else
      If *s\c >= 'A' And *s\c <= 'Z' : *s\c + 32 : EndIf
    EndIf
    last = *s\c : *s + SizeOf(Character) 
  Wend 
  ProcedureReturn string 
EndProcedure 

;-

i.l = 0
n.l = 50000
timer.l = 0
result.s = ""
requester.s = ""
teststring.s = ""
string.s = "the QUICK bRoWn fOx jUMPs ovER thE Lazy dog."

;-

teststring = string
timer = ElapsedMilliseconds()
For i = 0 To n : Capitalize_RemiMeier(@teststring) : Next 
requester + "remi_meier: " + Str(ElapsedMilliseconds()-timer) + "ms -> " + teststring + #LF$

teststring = string
timer = ElapsedMilliseconds()
For i = 0 To n : result = Capitalize_Flype2(teststring) : Next 
requester + "Flype2: " + Str(ElapsedMilliseconds()-timer) + "ms -> " + result + #LF$

teststring = string
timer = ElapsedMilliseconds()
For i = 0 To n : result = Capitalize_TheMexican(teststring) : Next 
requester + "TheMexican: " + Str(ElapsedMilliseconds()-timer) + "ms -> " + result + #LF$

teststring = string
timer = ElapsedMilliseconds()
For i = 0 To n : Capitalize_Xombie(@teststring) : Next 
requester + "Xombie: " + Str(ElapsedMilliseconds()-timer) + "ms -> " + teststring + #LF$

teststring = string
timer = ElapsedMilliseconds()
For i = 0 To n : result = Capitalize_Trond(teststring) : Next 
requester + "Trond: " + Str(ElapsedMilliseconds()-timer) + "ms -> " + result + #LF$

teststring = string
timer = ElapsedMilliseconds()
For i = 0 To n : result = Capitalize_Flype1(teststring) : Next 
requester + "Flype1: " + Str(ElapsedMilliseconds()-timer) + "ms -> " + result + #LF$

;-

MessageRequester("result", requester)

the larger (wider) is the code, the shorter is the procedure ! this is verified :D
Last edited by Flype on Tue Nov 07, 2006 11:53 pm, edited 2 times in total.
No programming language is perfect. There is not even a single best language.
There are only languages well suited or perhaps poorly suited for particular purposes. Herbert Mayer
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

Didn't know it was a speed contest. :twisted:

Code: Select all

Procedure.s Capitalize_TrondFast(string.s, separator.c = ' ')
  Protected Char.c
  Protected Char2.c
  Protected *s.Character
  Char = separator
  *s.Character = @string
  Char2 = *s\c
  While Char2
    If Char = separator
      If Char2 >= 'a' And Char2 <= 'z'
        *s\c - 32
      EndIf
    Else
      If Char2 >= 'A' And Char2 <= 'Z'
        *s\c + 32
      EndIf
    EndIf
    Char = Char2
    *s + SizeOf(Character)
    Char2 = *s\c
  Wend
  ProcedureReturn string
EndProcedure


Procedure.s Capitalize_Flype1(string.s, separator.c = ' ') ; 17 lines 
  Protected last.c = separator, *s.Character = @string 
  While *s\c 
    If last = separator 
      If *s\c >= 'a' And *s\c <= 'z' 
        *s\c - 32 
      EndIf 
    Else 
      If *s\c >= 'A' And *s\c <= 'Z' 
        *s\c + 32 
      EndIf 
    EndIf 
    last = *s\c 
    *s + SizeOf(Character) 
  Wend 
  ProcedureReturn string 
EndProcedure 

;- 

i.l = 0 
n.l = 1000000
timer.l = 0 
result.s = "" 
requester.s = "" 
teststring.s = "" 
string.s = "the QUICK bRoWn fOx jUMPs ovER thE Lazy dog." 

;- 

teststring = string 
timer = ElapsedMilliseconds() 
For i = 0 To n : result = Capitalize_Flype1(teststring) : Next 
requester + "Flype1: " + Str(ElapsedMilliseconds()-timer) + "ms -> " + result + #LF$ 

teststring = string 
timer = ElapsedMilliseconds() 
For i = 0 To n : result = Capitalize_TrondFast(teststring) : Next 
requester + "TrondFast: " + Str(ElapsedMilliseconds()-timer) + "ms -> " + result + #LF$ 

;- 

MessageRequester("result", requester)
User avatar
Flype
Addict
Addict
Posts: 1542
Joined: Tue Jul 22, 2003 5:02 pm
Location: In a long distant galaxy

Post by Flype »

lol, i can't do a faster one - but the time-difference is very small (1280ms/1180ms) :D

well done to save *s\c in a variable to avoid reading in memory...

but i'm sure you can do better :wink:
No programming language is perfect. There is not even a single best language.
There are only languages well suited or perhaps poorly suited for particular purposes. Herbert Mayer
Xombie
Addict
Addict
Posts: 898
Joined: Thu Jul 01, 2004 2:51 am
Location: Tacoma, WA
Contact:

Post by Xombie »

I didn't know it was a speed test, either :(

Code: Select all

Procedure.s Capitalize_TrondFast(string.s, separator.c = ' ') 
  Protected Char.c 
  Protected Char2.c 
  Protected *s.Character 
  Char = separator 
  *s.Character = @string 
  Char2 = *s\c 
  While Char2 
    If Char = separator 
      If Char2 >= 'a' And Char2 <= 'z' 
        *s\c - 32 
      EndIf 
    Else 
      If Char2 >= 'A' And Char2 <= 'Z' 
        *s\c + 32 
      EndIf 
    EndIf 
    Char = Char2 
    *s + SizeOf(Character) 
    Char2 = *s\c 
  Wend 
  ProcedureReturn string 
EndProcedure 
Procedure.s Capitalize_Flype1(string.s, separator.c = ' ') ; 17 lines 
  Protected last.c = separator, *s.Character = @string 
  While *s\c 
    If last = separator 
      If *s\c >= 'a' And *s\c <= 'z' 
        *s\c - 32 
      EndIf 
    Else 
      If *s\c >= 'A' And *s\c <= 'Z' 
        *s\c + 32 
      EndIf 
    EndIf 
    last = *s\c 
    *s + SizeOf(Character) 
  Wend 
  ProcedureReturn string 
EndProcedure 
Procedure.s Capitalize_Xombie2(HoldString.s, Delimiter.c = ' ')
   ;
   Protected HitDelimiter.l = #True
   ;
   Protected *Position.Character = @HoldString
   ;
   Repeat
      ;
      If *Position\c > 95
         ;
         If *Position\c < 123 : *Position\c = *Position\c - (32 * HitDelimiter) : EndIf
         ;
         HitDelimiter = #False
         ;
      ElseIf *Position\c > 64
         ;
         If *Position\c < 91 : *Position\c = *Position\c + (32 * (HitDelimiter ! 1)) : EndIf
         ;
         HitDelimiter = #False
         ;
      ElseIf *Position\c = Delimiter
         ;
         HitDelimiter = #True
         ;
      ElseIf *Position\c = 0
         ;
         ProcedureReturn HoldString
         ;
      EndIf
      ;
      *Position + SizeOf(Character)
      ;
   ForEver
   ;
EndProcedure
Procedure Capitalize_XombieInPlace(HoldString.l, Delimiter.c = ' ')
   ;
   Protected HitDelimiter.l = #True
   ;
   Protected *Position.Character = HoldString
   ;
   Repeat
      ;
      If *Position\c > 95
         ;
         If *Position\c < 123 : *Position\c = *Position\c - (32 * HitDelimiter) : EndIf
         ;
         HitDelimiter = #False
         ;
      ElseIf *Position\c > 64
         ;
         If *Position\c < 91 : *Position\c = *Position\c + (32 * (HitDelimiter ! 1)) : EndIf
         ;
         HitDelimiter = #False
         ;
      ElseIf *Position\c = Delimiter
         ;
         HitDelimiter = #True
         ;
      ElseIf *Position\c = 0
         ;
         Break
         ;
      EndIf
      ;
      *Position + SizeOf(Character)
      ;
   ForEver
   ;
EndProcedure
;- 

i.l = 0 
n.l = 1000000 
timer.l = 0 
result.s = "" 
requester.s = "" 
teststring.s = "" 
string.s = "the QUICK bRoWn fOx jUMPs ovER thE Lazy dog." 
;-
; Debug string+" : "+Str(Len(string))
; result = Capitalize_Xombie2(string)
; Debug result+" : "+Str(Len(result))
;- 
Delay(200)
teststring = string 
timer = ElapsedMilliseconds() 
For i = 0 To n : result = Capitalize_Flype1(teststring) : Next 
requester + "Flype1: " + Str(ElapsedMilliseconds()-timer) + "ms -> " + result + #LF$ 

Delay(200)
teststring = string 
timer = ElapsedMilliseconds() 
For i = 0 To n : result = Capitalize_TrondFast(teststring) : Next 
requester + "TrondFast: " + Str(ElapsedMilliseconds()-timer) + "ms -> " + result + #LF$ 

Delay(200)
teststring = string 
timer = ElapsedMilliseconds() 
For i = 0 To n : result = Capitalize_Xombie2(teststring) : Next 
requester + "Xombie2: " + Str(ElapsedMilliseconds()-timer) + "ms -> " + result + #LF$ 

Delay(200)
teststring = string 
timer = ElapsedMilliseconds() 
For i = 0 To n : teststring = string : Capitalize_XombieInPlace(@teststring) : Next 
requester + "XombieInPlace: " + Str(ElapsedMilliseconds()-timer) + "ms -> " + teststring + #LF$ 

MessageRequester("result", requester) 
;-
See, Trond? I put two in there from me. One that passes the string address and modifies in place (you said cheating) and one that follows both of yours by passing a string parameter. Juuuuust for you :D

Even with continuously reassigning the string value in the test loop, the in-place modification is much faster.

But the other one is still pretty darn fast too :)
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4789
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Post by Fangbeast »

I may not understand it but I love watching the children play :evil: :evil: :evil: :evil:

Noooooo, put down that gun!
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post by PB »

> it's a pity that purebasic do not have the 'If' / 'Then' statement because
> i could save 2 more lines :D

Oh, but it does: http://www.purebasic.fr/english/viewtopic.php?t=20619 ;)
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
AND51
Addict
Addict
Posts: 1040
Joined: Sun Oct 15, 2006 8:56 pm
Location: Germany
Contact:

Post by AND51 »

Maybe, you've beaten me, but you must agree to me when I say: A recursive call makes to code smaller and faster, I had an excellent idea... 8)
PB 4.30

Code: Select all

onErrorGoto(?Fred)
techjunkie
Addict
Addict
Posts: 1126
Joined: Wed Oct 15, 2003 12:40 am
Location: Sweden
Contact:

Post by techjunkie »

What about this... :lol: :lol:

Code: Select all

  Procedure.s CapString(st.s)
    For i= 1 To FindString(st.s, " ", 1) + 1
      temp.s = temp.s + UCase(Left(StringField(st.s, i, " "), 1)) + LCase(Mid(StringField(st.s, i, " "), 2, Len(StringField(st.s, i, " ")))) + " "
    Next
    ProcedureReturn temp.s
  EndProcedure
  
  Debug CapString("tRonD LikE BanAnas, BuT NOT aPPlEs!")
3 lines (4 with ProcedureReturn)... :wink:

[EDIT]

Correction, removing the last " "...

Code: Select all

Procedure.s CapString(st.s)
    For i= 1 To FindString(st.s, " ", 1) + 1
      temp.s = temp.s + UCase(Left(StringField(st.s, i, " "), 1)) + LCase(Mid(StringField(st.s, i, " "), 2, Len(StringField(st.s, i, " ")))) + " "
    Next
    ProcedureReturn Mid(temp.s, 1, Len(temp.s) - 1)
  EndProcedure
  
  Debug CapString("tRonD LikE BanAnas, BuT NOT aPPlEs!")
Image
(\__/)
(='.'=) This is Bunny. Copy and paste Bunny into your
(")_(") signature to help him gain world domination.
AND51
Addict
Addict
Posts: 1040
Joined: Sun Oct 15, 2006 8:56 pm
Location: Germany
Contact:

Post by AND51 »

Better:

Code: Select all

ProcedureReturn Mid(temp.s, 1, Len(temp.s) - 1) ; no, this is too slow

;Better: Right()
Procedurereturn Right(temp, Len(temp)-1)

; The best way: PeekS() [unicode-compatible!!!)
ProcedureReturn PeekS(@temp+1+#PB_Compiler_Unicode)
PB 4.30

Code: Select all

onErrorGoto(?Fred)
Post Reply