PDA

View Full Version : String Stuffz



n3ss3s
01-05-2008, 08:37 PM
For all your text manipulation needs:


program New;

Var
A: TStringArray;

Function Explode(Sep, Text: String): TStringArray;
Var
L, Le, I: Integer;
Begin
SetArrayLength(Result, 1);
L := Length(Text);
For I := 1 To L Do
If Text[i] <> Sep Then
Begin
Le := High(Result);
Result[Le] := Result[Le] + Text[i];
End Else
SetArrayLength(Result, GetArrayLength(Result) + 1);
End;


Function AlphabeticalPos(Letter: String): Integer;
Var
I: Integer;
A, L: String;
Begin
A := 'abcdefghijklmnopqrstuvwxyz';
L := LowerCase(Letter);
For I := 1 To 26 Do // Case would've been boring =]
If A[i] = L Then
Begin
Result := I;
Exit;
End;
End;


Function OrderToAlphabet(Text: String): TStringArray;
//* The A's will be found in Result[0] etc.
Var
I, L, AP: Integer;
Begin
SetArrayLength(Result, 26);
L := Length(Text);
For I := 1 To L Do
Begin
AP := AlphabeticalPos(Text[i]);
Result[AP - 1] := Result[AP - 1] + Text[i];
End;
End;


Var
I: Integer;

begin
A := OrderToAlphabet('ababcdcd');
For I := 0 To High(A) Do
Writeln(A[i]);

end.


EDIT: Explode isn't originally my idea ofcourse, I just wanted to try to do it that way :)

R0b0t1
01-05-2008, 09:54 PM
Bubble. Sorting. Is. Slow. As. Hell.

jhildy
01-06-2008, 01:29 AM
what does explode exactly do?

Boreas
01-06-2008, 04:53 AM
program New;

Var
A: String;
B: TStringArray;

Function Explode(Sep, Text: String): TStringArray;
Var
L, Le, I: Integer;
Begin
SetArrayLength(Result, 1);
L := Length(Text);
For I := 1 To L Do
If Text[i] <> Sep Then
Begin
Le := High(Result);
Result[Le] := Result[Le] + Text[i];
End Else
SetArrayLength(Result, GetArrayLength(Result) + 1);
End;
var i:integer;
begin
A := 'hello/nice/to/meet/you';
B := Explode('/',A);
for i:=0 to 4 do
writeln(B[i]);
end.


Successfully compiled
hello
nice
to
meet
you
Successfully executed

Wizzup?
01-06-2008, 10:45 AM
SRL/Misc/arrayloader.scar

;)

n3ss3s
01-06-2008, 11:19 AM
Sweet, but tell me, in what occasion I would have to turn string to tpointarray instead of intz?