Entry
TSE: String: Check: How to check if all single characters are unique in a string?
Nov 7th, 2006 16:30
Knud van Eeden,
----------------------------------------------------------------------
--- Knud van Eeden --- 08 November 2020 - 01:25 am -------------------
TSE: String: Check: How to check if all single characters are unique
in a string?
---
Method: Use counting of the single characters. Only 1 should be found
each time.
You take one after one each character of the given (sorted or not
sorted) string and search then in the given string if you find exactly
1
occurrence of this character.
If all characters are occur only once then the given string contains
only unique characters.
===
--- cut here: begin --------------------------------------------------
// library: string: get: count (filenamemacro=getstgco.s) [kn, ri, we,
08-11-2020 01:15:00]
INTEGER PROC FNStringGetCountI( STRING containS, STRING searchS )
INTEGER I = 1 - 1
INTEGER countI = 1 - 1
INTEGER foundB = FALSE
INTEGER beginI = 1
INTEGER endI = Length( containS )
INTEGER lengthSearchI = Length( searchS )
REPEAT
I = Pos( searchS, containS[beginI..endI] )
foundB = ( I > 0 )
IF foundB
countI = countI + 1
beginI = beginI + I + lengthSearchI - 1
ENDIF
UNTIL NOT foundB
RETURN( countI )
END
INTEGER PROC FNStringCheckCharacterUniqueAllB( STRING s )
INTEGER I = 0
INTEGER lengthI = Length( s )
INTEGER duplicateB = FALSE
INTEGER countI = 0
STRING cS[255] = ""
I = 1 - 1
REPEAT
I = I + 1
cS = SubStr( s, I, 1 )
countI = FNStringGetCountI( s, cS )
duplicateB = ( NOT ( countI == 1 ) )
UNTIL ( duplicateB ) OR ( I >= lengthI )
RETURN( NOT duplicateB )
END
PROC Main()
Message( FNStringCheckCharacterUniqueAllB( "ABC" ) ) // gives TRUE
because all characters occur exactly once
Message( FNStringCheckCharacterUniqueAllB( "ABBC" ) ) // gives FALSE
because the B occurs 2 times
END
<F12> Main()
--- cut here: end ----------------------------------------------------
===
Internet: see also:
---
----------------------------------------------------------------------