Richard
2010-12-16 18:29:21
I'm writing a function which splits a string into all the possible sequential counterparts, eg the string "box" has the possibility of "b", "o", "x", "bo", "ox" and "box". So all characters must be touching.
I've got a function which (in my head) should work theoretically, but doesn't when ran, displaying many blank strings, and 1 or 2 repeated sections of the string. If I'm right, the error is where the section of string is actually written to the array.
The code:
(this will have a practical use eventually, and hopefully won't be as inefficient as I think it will…)
Thanks
I've got a function which (in my head) should work theoretically, but doesn't when ran, displaying many blank strings, and 1 or 2 repeated sections of the string. If I'm right, the error is where the section of string is actually written to the array.
The code:
program StringSplit;
var
string1: string;
stringArray: Array of String;
function SplitString(str: string): Array of String;
var
i, n, possibles: integer;
begin
n := Length(str);
for i := Length(str) downto 0 do
possibles := possibles + n;
SetArrayLength(Result, possibles - 1);
for i := 0 to Length(str) - 1 do //for each position in the string
for n := 0 to Length(str) - 1 do //for each possible length of the string
Result[i+n] := Copy(str, i, n); //writes string part into result array
for i := 0 to high(Result) do
writeLN(Result[i]);
end;
begin
stringArray := SplitString('string');
string1 := stringArray[random(High(stringArray))];
WriteLN(string1);
end. (this will have a practical use eventually, and hopefully won't be as inefficient as I think it will…)
Thanks