Click or drag to resize

CharMix Function

Return a string whose odd-numbered characters and even-numbered characters are from 2 different strings.

Namespace:  XSharp.Core
Assembly:  XSharp.Core (in XSharp.Core.dll) Version: 2.4a
Syntax
 FUNCTION CharMix(
	cOdd AS STRING,
	cEven AS STRING
) AS STRING
View Source

Parameters

cOdd
Type: String
The string whose characters will constitute the odd-numbered characters in the returned string.
cEven
Type: String
The string whose characters will constitute the even-numbered characters in the returned string.

Return Value

Type: String
A string in which characters from <cOdd> and <cEven> appear alternately.
The length of <cOdd> determines the length of the returned string: the length is twice the length of <cOdd>, assuming that <cEven> contains at least one character: A longer <cEven> string is cut down to the length of <cOdd>. A shorter <cEven> is processed from beginning to end, wrapping to the beginning again until there are no more characters in <cOdd>. If <cEven> contains less than one character, a NULL_STRING is returned.
Remarks
CharMix() provides a convenient way to recombine strings extracted from CharOdd() and CharEven().
Examples
This example mixes 2 strings:
X#
1? CharMix("13579", "24680")    // 1234567890
This example  shuffles the cards in a game:
X#
 1Function Start()
 2    LOCAL cPoker AS STRING
 3    cPoker := "AKJQ0123456789"
 4    ? Shuffle(cPoker, 10)    // Q37A048K159J26
 5    RETURN TRUE
 6FUNCTION Shuffle(cDeck AS STRING, ;
 7        siShuffles AS SHORTINT) AS STRING PASCAL
 8    // Assumes even number of cards.
 9    // Length of cDeck is divisible by 2
10    LOCAL siCounter AS SHORTINT
11    LOCAL siCards AS SHORTINT
12    siCards := SLen(cDeck)
13    // First and last cards are not shuffled;
14    // so add/pad blank cards
15    siCards += 2
16    cDeck := PadC(cDeck, siCards)
17    FOR siCounter := 1 UPTO siShuffles
18        // Cut the deck in half and shuffle
19        cDeck := CharMix(Substr3(cDeck, 1,;
20        siCards/2), Substr2(cDeck, (siCards/2)+1))
21    NEXT
22    RETURN Substr3(cDeck, 2, siCards-2)
23    // Exclude added blank cards
See Also