There are many unnecessary variables in your code when you declare a function
is required to determine the type of return (in your case is Integer
). The Delphi
automatically creates a variable in the scope of function
calling for Result
and its type is the same as the return. So you only need this variable and one more to make the increment in the for
.
Ansiuppercase:
This function causes all characters to be replaced by their uppercase version. When you do Str[1]
is returned a Char
and the comparison between Char
is key sensitive, ie will differentiate uppercase and lowercase.
Inc:
This function sums and assigns the new value to the variable passed, when you do not pass the second parameter that is the number that will be added, it assumes that this number is 1. I have a certain schism with this function I’ve had problems with it, but when I solve it uses itthere I make a point to pass the second parameter even if it is 1, to avoid problems.
Below is an example that worked well:
uses
SysUtils;
function ContarLetras(Str: String): Integer;
var
i: Integer;
begin
Result := 0;
for i:=1 to length(str) do
if Str[i] in ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'] then
Inc(Result);
end;
begin
Writeln(IntToStr(ContarLetras('aa11')));
Readln;
end.
Essentially it has to do in hand, even more being an algorithm exercise.
– Maniero
Which variable is getting this value at the end?
– Roberto de Campos
It is the variable Countletters. I use an equal function to calculate numbers from 0 to 9 and it works, but with letters it does not compute.
– lukkicode