Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count characters and substring in words

Tags:

delphi

I have to read an unknown quantity of words and terminate the program when the word "last" is typed in. I have to:

  • Display the number of names that begin a with "S".
  • Display the number of names that end a with "d".
  • Display the number of names that end with "anne".

Problem: The only problem I have is displaying the number of names that end with "anne". It displays 0, except if I only enter the word "anne", then it displays 1.

Note:

  • We haven't started with arrays.
  • This is work I'm practising before I write my test, it's not homework that should be handed in.

My code:

unit Ess_U;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ComCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    redOut: TRichEdit;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
Var wrd : string;
    Scount, dcount, Namecount : integer;
begin
redOut.clear;
   Scount := 0;
   dcount := 0;
   Namecount := 0;


   repeat
    wrd := inputbox ('Input words', 'Enter any word', '');

    if (Pos('S',wrd) > 0) AND (wrd[1] = 'S') then
     begin
     inc(Scount);
     end

    else if (Pos('d',wrd) > 0) AND (wrd[length(wrd)] = 'd') then
     begin
     inc(dcount);
     end

    else if copy(wrd, length(wrd)-4,4) = 'anne' then
     begin
     inc(Namecount);
     end;

    until (wrd = 'last');

    redOut.Lines.Add ('The number of names that begin with the letter "S" is ' + inttostr(Scount));
    redOut.Lines.Add ('The number of names that end with the letter "d"  is ' + inttostr(dcount));
    redOut.Lines.Add ('The number of names that begin with "anne" is ' + inttostr(Namecount));
end;

end.

enter code here
like image 680
Amber Avatar asked Jan 29 '26 05:01

Amber


1 Answers

You've already had an answer that shows you another way to do what you were attempting, using library functions. That's fine (assuming your Delphi version has them), but there's an unresolved aspect to your question.

You said "Problem: The only problem I have is displaying the number of names that end with "anne". It displays 0, except if I only enter the word "anne", then it displays 1."

Now, a key part of coding is learning to debug, and a key part of that is accurate observation and knowing to construct a reproducible test case.

Try this:

Change the first line of your repeat loop to read:

wrd := 'xanne'; //inputbox ('Input words', 'Enter any word', '');

and change the test for 'anne' to read

else begin
  wrd := copy(wrd, length(wrd)-4,4);
  Caption := wrd;
  if wrd = 'anne' then
    begin
     inc(Namecount);
    end;
end;

then, put a breakpoint on the line

  wrd := copy(wrd, length(wrd)-4,4);

and press F9 to compile and run your program.

When the debugger stops at the breakpoint, keep pressing F8 (to single-step through the code.

You'll soon see what's wrong, namely that

 copy(wrd, length(wrd)-4,4)

does not equal 'anne' when wrd starts off as 'xanne'. I'll leave you to work out why not, because I think you'll find that a bit more instructive than just finding out about a new-to-you library function.

Btw, this sort of thing tends to happen when you try testing a program by typing inputs over and over. That's why I've said to modify you code temporarily, so that you're starting from a known input, not one you may have mistyped (or even had the CapsLock on by mistake and not noticed).

like image 120
MartynA Avatar answered Jan 31 '26 00:01

MartynA



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!