Hi, maybe can you can help me. I need to write a pascal a program which would read text file from hard disk and would sort all text's words to 3 columns, and would save results to the another file. Let's say that we have no more than 1000 words in the given text file.

Something like that, you have a text:

sdfsdf sdfsdf dfdf sdfsdf, sdfsdf. sdfssdfdf sdfsdf, sdfsdf. sdfsd sdfsndf dfd ....

And I must get a result to 3 colums:

word word word
word word word
word word word
word word word
... .... ....

I would be very thankful if you could help me! help me! ;)[/B]

Dani AI

Generated

A clear, robust approach is to read the file line by line, build word tokens by treating any non-alphanumeric character as a separator, store up to the 1000 words in an array, then write them out three per line (left-to-right) to the output file. asked for this behavior; asked to see code; pointed to an earlier thread — below is a compact, working Free Pascal example that demonstrates the tokenization and 3-column output. It normalizes words to lowercase and stops after 1000 tokens.

program WordsToThreeColumns;
{$mode objfpc}{$H+}
uses SysUtils;
const MaxWords = 1000;
var
  inf, outf: TextFile;
  line, cur, s1, s2, s3: string;
  words: array[1..MaxWords] of string;
  i, wcount: Integer;
  ch: Char;

procedure AddWord(const s: string);
begin
  if (s <> '') and (wcount < MaxWords) then
  begin
    Inc(wcount);
    words[wcount] := s;
  end;
end;

begin
  AssignFile(inf, 'input.txt'); Reset(inf);
  AssignFile(outf, 'output.txt'); Rewrite(outf);
  wcount := 0;
  while not Eof(inf) do
  begin
    ReadLn(inf, line);
    cur := '';
    for i := 1 to Length(line) do
    begin
      ch := line[i];
      if ch in ['A'..'Z','a'..'z','0'..'9'] then cur := cur + ch
      else if cur <> '' then begin AddWord(LowerCase(cur)); cur := ''; end;
    end;
    if cur <> '' then begin AddWord(LowerCase(cur)); cur := ''; end;
  end;

  i := 1;
  while i <= wcount do
  begin
    if i <= wcount then s1 := words[i] else s1 := '';
    if i+1 <= wcount then s2 := words[i+1] else s2 := '';
    if i+2 <= wcount then s3 := words[i+2] else s3 := '';
    Writeln(outf, s1, #9, s2, #9, s3);
    i := i + 3;
  end;
  CloseFile(inf); CloseFile(outf);
end.

Notes: digits are treated as part of words by the code above. To sort alphabetically or remove duplicates, load words into a TStringList and use its Sort and CustomSort facilities. For UTF-8 text or compiler differences, replace the simple char checks with proper Unicode-aware routines.

Recommended Answers

All 2 Replies

If you could post the code you have so far I would be happy to help you.

Was the help in this thread not good enough for you?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.