There are many unnecessary variables in your code, when you declare a function
you must determine the return type (in your case it is Integer
). Delphi
automatically creates a variable in the scope of function
called Result
and its type is the same as return. So you only need this variable and one more to make the increment in for
.
AnsiUpperCase:
This function causes all characters to be replaced with their uppercase version. When you do Str[1]
a Char
is returned and the comparison between Char
is key sensitive, that is, it will differentiate between upper and lower case.
Inc:
This function adds and assigns the new value to the passed variable, when the second parameter is not passed, which is the number that will be added, it assumes that this number is 1. I have a certain schism with this function I already had problems with it, but when I use it I make it a point to pass the second parameter even if it is 1, to avoid problems.
Here 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.