Reputation: 171
I have to read an unknown quantity of words and terminate the program when the word "last" is typed in. I have to:
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:
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
Upvotes: 3
Views: 280
Reputation: 5438
You should be using StartsText
and EndsText
from StrUtils
to check if a string starts or ends with a text, instead of manually calling Pos
and Copy
. (or AnsiStartsText
and AnsiEndsText
)
Add StrUtils
to your uses clause and change the section with conditions like that:
...
uses StrUtils, ...;
...
if (StartsText('S', wrd)) then
begin
Inc(Scount);
end
if (EndsText('d', wrd)) then
begin
Inc(dcount);
end
if (StartsText('anne', wrd)) then
begin
Inc(Namecount);
end;
The StrUtils
unit has the following useful routines:
- StartsText - determines if the substring
ASubText
begins the stringAText
, case insensitive comparison;- StartsStr - same as
StartsText
, but case sensitive;- EndsText - determines if the substring
ASubText
ends the stringAText
, case insensitive comparison;- EndsStr - same as
EndsStr
, but case sensitive;
Upvotes: 4
Reputation: 30715
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).
Upvotes: 6