ne besser ist es keinesfalls, aber ich denke ich hätte es irgendwi hinbekommen.
also ich hatte es so(mit nem button und nem memo:
Delphi-Quellcode:
type
TWordCount = Record
word: string;
count: integer;
end;
...
procedure TForm1.Button1Click(Sender: TObject);
const
FilePath: string = 'C:\Test.txt';
sep: char = ' ';
var
tf: TextFile;
TempStr, TempWord: string;
WordCount: Array of TWordCount;
i: integer;
IsInArray: boolean;
begin
AssignFile(tf, FilePath);
Reset(tf);
while not EOF(tf) do
begin
ReadLn(tf, TempStr);
TempStr := Trim(TempStr);
TempStr := TempStr + ' ';
while pos(sep, TempStr) > 0 do
begin
IsInArray := false;
TempWord := Copy(TempStr, 1, Pos(sep, TempStr)-1);
Delete(TempStr, 1, Pos(sep, TempStr));
for i := low(WordCount) to high(WordCount) do
begin
if WordCount[i].word = TempWord then
begin
IsInArray := true;
inc(WordCount[i].count);
end;
end;
if not IsInArray then
SetLength(WordCount, Length(WordCount)+1);
WordCount[high(WordCount)].word := TempWord;
WordCount[high(WordCount)].count := 1;
end;
end;
for i := low(WordCount) to high(WordCount) do
Memo1.Lines.Add('Wort "' + WordCount[i].word + '" ist ' + IntToStr(WordCount[i].count) + ' mal enthalten');
CloseFile(tf);
SetLength(WordCount, 0);
WordCount := nil;
end;
ich glaube das array hab ich am ende falsch freigegeben oder?
Und noch ne Frage, beim ausführen des programms werden mir manche einträge doppelt angezeigt.
z.b wenn das die Textdatei ist:
Zitat:
hallo
hallo2 3
hi
hallo2
3
hi
hi
hi
hi
hi
hi
wird mir beim buttonclick des programms aber nur:
Zitat:
Wort "hallo" ist 1 mal enthalten
Wort "hallo2" ist 2 mal enthalten
Wort "3" ist 2 mal enthalten
Wort "3" ist 1 mal enthalten
Wort "hi" ist 1 mal enthalten
angezeigt. Kannst du mir sagen woran das liegt?