program SplitStringIntoChunks;
type
TStringArray = array of string; // Define a named type for the array
function SplitString(const str: string; chunkSize: Integer): TStringArray; // Use the named type
var
i, len, chunkCount, copyLength: Integer;
result: TStringArray; // Use the named type
begin
len := Length(str);
chunkCount := (len + chunkSize - 1) div chunkSize; // Calculate the number of chunks
SetLength(result, chunkCount); // Resize the array to store chunks
for i := 0 to chunkCount - 1 do
begin
// Calculate the length of the substring to copy
copyLength := chunkSize;
if (i * chunkSize + chunkSize) > len then
copyLength := len - (i * chunkSize);
// Extract substring of chunkSize or remaining characters
result[i] := Copy(str, i * chunkSize + 1, copyLength);
end;
SplitString := result; // Return the result array
end;
var
str: string;
chunks: TStringArray; // Use the named type
i: Integer;
begin
str := 'abcdefghijk';
// Split the string into chunks
chunks := SplitString(str, 2);
WriteLn('Chunks of two characters:');
for i := Low(chunks) to High(chunks) do
begin
WriteLn(chunks[i]);
end;
end.
(*
run:
Chunks of two characters:
ab
cd
ef
gh
ij
k
*)