Comparing strings char by char

Just starting out? Need help? Post your questions and find answers here.
firace
Addict
Addict
Posts: 947
Joined: Wed Nov 09, 2011 8:58 am

Comparing strings char by char

Post by firace »

I want to compare 2 strings of equal length and find which characters differ and which are equal.
I managed to do this with the below code (note: must be compiled as ASCII), but one little detail that I don't understand is why I need to subtract 1 from @r0+i in the PokeC. Could anyone with a sharper brain answer this one? :)

Code: Select all

s1.s = "24cdef5e8db4afca39a4198d4df5d6e4"
s2.s = "14cd9f5e8db2afca39a41c8d4dfcd6eb"
r0.s = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

For i = 0 To Len (s1) 
  If Mid(s1,i,1) =  Mid(s2,i,1) : PokeC (@r0+i-1,'=') : EndIf
Next
    
Debug r0
Thanks!
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3943
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Comparing strings char by char

Post by wilbert »

PB string positions start with 1, not 0 so your loop should be from 1 to Len(s1)
Windows (x64)
Raspberry Pi OS (Arm64)
firace
Addict
Addict
Posts: 947
Joined: Wed Nov 09, 2011 8:58 am

Re: Comparing strings char by char

Post by firace »

wilbert wrote:PB string positions start with 1, not 0 so your loop should be from 1 to Len(s1)
Ouch, of course!! Thanks! :)
User avatar
Lord
Addict
Addict
Posts: 907
Joined: Tue May 26, 2009 2:11 pm

Re: Comparing strings char by char

Post by Lord »

Why not using PeekC()?

Code: Select all

s1.s = "24cdef5e8db4afca39a4198d4df5d6e4"
s2.s = "14cd9f5e8db2afca39a41c8d4dfcd6eb"
r0.s = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

AdrS1.i=@s1
AdrS2.i=@s2
AdrR0.i=@r0
L.i=Len(s1)-1

For i = 0 To L
  If PeekC(AdrS1+i) =  PeekC(AdrS2+i) : PokeC (AdrR0+i,'=') : EndIf
Next

Debug r0
Image
User avatar
STARGÅTE
Addict
Addict
Posts: 2260
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: Comparing strings char by char

Post by STARGÅTE »

This code doesn't work with unicode.

Here an other example:

Code: Select all

Structure CharacterArray
	C.C[0]
EndStructure

s1.s = "24cdef5e8db4afca39a4198d4df5d6e4"
s2.s = "14cd9f5e8db2afca39a41c8d4dfcd6eb"
r0.s = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

*AdrS1.CharacterArray = @s1
*AdrS2.CharacterArray = @s2
*AdrR0.CharacterArray = @r0

Define I = 0

While *AdrR0\C[I]
	If *AdrS1\C[I] = *AdrS2\C[I]
		*AdrR0\C[I] = '='
	EndIf
	I + 1
Wend

Debug r0
@IdeasVacuum: My code is unicode compatible
Last edited by STARGÅTE on Sat May 16, 2015 11:27 am, edited 1 time in total.
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Comparing strings char by char

Post by IdeasVacuum »

All these solutions rely on using ASCII - but what about Unicode strings?
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
said
Enthusiast
Enthusiast
Posts: 342
Joined: Thu Apr 14, 2011 6:07 pm

Re: Comparing strings char by char

Post by said »

Hi,
An alternative that works with ascii/unicode (and faster than Poke/Peek)

Code: Select all

s1.s = "24cdef5e8db4afca39a4198d4df5d6e4"
s2.s = "14cd9f5e8db2afca39a41c8d4dfcd6eb"
r0.s = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"


*C1.Character = @s1
*C2.Character = @s2
*C3.Character = @r0
While *C1\c
    If *C1\c = *C2\c : *C3\c = '=' : EndIf
    *C1 + SizeOf(Character)
    *C2 + SizeOf(Character)
    *C3 + SizeOf(Character)
Wend
Debug r0
edit: updated to include the *Ci + .... (lost with copy/paste)
User avatar
Lord
Addict
Addict
Posts: 907
Joined: Tue May 26, 2009 2:11 pm

Re: Comparing strings char by char

Post by Lord »

STARGÅTE wrote:This code doesn't work with unicode.
...[/code]
The premise was:
firace wrote:...
(note: must be compiled as ASCII)
...
And not everybody wants/needs unicode.
Leave BASIC basic.
Image
User avatar
STARGÅTE
Addict
Addict
Posts: 2260
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: Comparing strings char by char

Post by STARGÅTE »

PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
User avatar
Lord
Addict
Addict
Posts: 907
Joined: Tue May 26, 2009 2:11 pm

Re: Comparing strings char by char

Post by Lord »

As I already stated: BAD NEWS!
Keep BASIC basic!
Image
User avatar
Tenaja
Addict
Addict
Posts: 1959
Joined: Tue Nov 09, 2010 10:15 pm

Re: Comparing strings char by char

Post by Tenaja »

Lord wrote:Keep BASIC basic!
ASCII vs Unicode has nothing to do with BASIC being basic.

Please imagine what you are saying, from a the perspective of other people...
Lord wrote:Keep BASIC English.
Because English is one of the few languages without unicode characters.
User avatar
Lord
Addict
Addict
Posts: 907
Joined: Tue May 26, 2009 2:11 pm

Re: Comparing strings char by char

Post by Lord »

Tenaja wrote:...
Lord wrote:Keep BASIC English.
Because English is one of the few languages without unicode characters.
So what?
My native language is not English.
Image
marroh
User
User
Posts: 72
Joined: Wed Aug 06, 2008 8:21 am

Re: Comparing strings char by char

Post by marroh »

[offtopic on]
Lord wrote:And not everybody wants/needs unicode.
+1

[offtopic off]
PureBASIC v5.41 LTS , Windows v8.1 x64
Forget UNICODE - Keep it BASIC !
User avatar
Tenaja
Addict
Addict
Posts: 1959
Joined: Tue Nov 09, 2010 10:15 pm

Re: Comparing strings char by char

Post by Tenaja »

marroh wrote:[offtopic on]
Lord wrote:And not everybody wants/needs unicode.
+1

[offtopic off]
Your complaints are moot, considering the decisions of the dev team. OTOH, you can always use a "legacy" version of PB.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8452
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: Comparing strings char by char

Post by netmaestro »

Just one point to consider, depending upon how many string comparisons would test as equal you might realize a considerable speed gain by first doing a hash check on the strings and if they are the same, stop there. If not, then compare char by char.
BERESHEIT
Post Reply