Count Number of Letters in a String - Delphi

0

I am creating a program where you will need to display the number of letters (A, b, C ..Z; a..z, a, â, ã) present in a String.

I'm using the following code:

function ContarLetras(Str: String): Integer;
var
    Ret, i: Integer;
begin
  Ret:=0;
  Str:= AnsiUpperCase(Str);
    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
       begin
       Inc(Ret);
       ContarLetras:=Ret;
       end;
end;

However, running the program displays a number that has nothing to do with the count (eg 79649600).

Where can the error be?

    
asked by anonymous 28.07.2018 / 02:04

3 answers

1

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.
    
28.07.2018 / 03:38
1

To solve your problems with special characters, you can declare the unit "System.Character" and change its function to:

function ContarLetras(Str: String): integer;
var
  umChar: char;
begin
  result := 0;
  for umChar in Str do
    if IsLetter(umChar) then
      Inc(result);
end;
    
01.08.2018 / 20:17
0

Well, two things that I think are not right (1) are you converting to uppercase and comparing with lowercase characters, and (2) the return should be set outside the loop.

As I have not worked with Delphi for years, I can not test, but see if that's it:

function ContarLetras(Str: String): Integer;
var
    Ret, i: Integer;
begin
  Ret:=0;
  Str:= AnsiUpperCase(Str);
  for i:=1 to length(str) do
  begin
    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
    begin
      Inc(Ret);
    end;
  end;
  ContarLetras:=Ret;
end;
    
28.07.2018 / 03:27