Js98
Js98

Reputation: 9

How to count the length of each word in Delphi?

I am a Grade 10 IT student, so I am relatively new to Delphi. I need to make a program that receives a sentence as input from an edit box. The number of words and the length of each. I can find the position of the first space in the sentence but do not know how to find each space.

Can anyone help?

Upvotes: 1

Views: 2189

Answers (3)

LU RD
LU RD

Reputation: 34889

You can use the TStringList class to split a string into substrings with a specified Delimiter property and the DelimitedText method.

program Project1;

{$APPTYPE CONSOLE}

uses
  System.SysUtils,Classes;

procedure CheckString( const someText: String; var wordLength: TArray<Integer>);
var
  aList: TStringList;
  i: Integer;
begin
  aList := TStringList.Create;
  try
    aList.Delimiter := ' ';  // Define the delimiter
    aList.DelimitedText := someText;  // Split the text
    SetLength(wordLength,aList.Count);  // Set the result word count
    for i := 0 to Pred(aList.Count) do
    begin
      wordLength[i] := Length(aList[i]);    // Set the word length
    end;
  finally
    aList.Free;
  end;
end;

var
  s: String;
  counter: TArray<Integer>;
  i: Integer;
begin
  s := 'This is a  test.';
  CheckString(s,counter);
  WriteLn('Found words:',Length(counter));
  for i := 0 to Pred(Length(counter)) do
    WriteLn(counter[i]);
  ReadLn;
end.

If you are using an older version of Delphi, replace TArray<Integer> with TIntegerDynArray defined in the Types unit. And in old Delphi-7 you must declare it yourself:

Type
  TIntegerDynArray = array of Integer;

If you want to exclude some characters from your string before the split, use this function:

function StripChars(const aSrc, aCharsToStrip: string): string;
var
  i: Integer;
begin
  Result := aSrc;
  for i := 1 to Pred(Length(aCharsToStrip) do
    Result := StringReplace(Result, aCharsToStrip[i], '', 
      [rfReplaceAll, rfIgnoreCase]);
end;

// Example call
myString := StripChars(s,'.,!?;');

Upvotes: 2

Enny
Enny

Reputation: 771

It depends which Version of Delphi do you use. Here are 2 possible solutions:

  1. All Delphi Versions:
var input : string;
    aword : string;
    r: Integer;
    allowedchars : set of 'A'..'z';
begin
  allowedchars:=['A'..'z'];
  ListBox1.Clear;
  input:='This is my test sentence. Feel free to count words, or let it count!';
  for r := 1 to Length(input) do begin
    if(input[r] in allowedchars)then begin
      aword:=aword+input[r];
    end else begin
      if(length(aword)>0)then
      ListBox1.Items.Add(Format('%s: %d',[aword,Length(aword)]));
      aword:='';
    end;
  end;
end;
  1. Newer Delphi Versions, from XE3 on
var input : string;
    words : TArray<string>;
    aword : string;
begin
  ListBox1.Clear;
  //1. Use the Split Helper Method, from XE3 and newer Delphis
  input:='This is my test sentence. Feel free to count words, or let it count!';
  words:=input.Split(['.',';',',','!','?',' '],TStringSplitOptions.ExcludeEmpty);
  ListBox1.Items.Add(Format('Words: %d',[Length(words)]));
  for aword in words do begin
    ListBox1.Items.Add(Format('%s: %d',[aword,aword.Length]));
  end;
end;

Upvotes: 4

Magoo
Magoo

Reputation: 79957

You may find the posex function very useful...if it's part of D7

Otherwise, look for the delete function or copy function.

Upvotes: 1

Related Questions