Count Number of Letters in a String - Delphi

Asked

Viewed 1,538 times

0

I am creating a program where it will be necessary to show the amount of letters (A, b, C ..Z; a..z, à,â,ã) 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;

But when running the program a number is displayed that has nothing to do with counting (e.g.: 79649600).

I wonder where the mistake might be?

  • Essentially it has to do in hand, even more being an algorithm exercise.

  • Which variable is getting this value at the end?

  • 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.

3 answers

1

To solve your problems with the special characters, you can declare 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;
  • Explaining the function: forin > loops, already adding the character of the string into a variable. isLetter > Delphi function that checks if char is a letter (upper case, lower case, accent or not).

1

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.
  • Really, I was wiping the function here and it looked like yours. The only problem is it doesn’t count characters with accent.

  • To count the accented characters you will have to add them in the array also. This has to be done because a is different from á and vice versa.

  • Thank you Roberto Campos and everyone who contributed.

  • If that was the answer that helped solve the problem, mark it as the solution to the question.

  • 1

    You can also summarize in as follows: if Str[i] in ['a'..'z'] then and for the accents just add separated with comma, example: ['a'..'z', 'á', 'à', 'ã'........]

  • 1

    Following the same @Melissa example you can put an 'A' at the beginning and a 'z' at the end to accept both upper and lower case characters. Would look like this if Str[i] in ['A'..'z'] then. It is worth noting that this form accepts the [ characters and the crase, because in the ASCII table they are between code 65 (the uppercase "A" and 122 (the lowercase "z"). Another alternative is if Str[i] in ['A'..'Z', 'a'..'z'] then.

Show 1 more comment

0

Well, two things I think are not right (1) is you convert to uppercase and compare with lowercase characters, and (2) the return should be set out of the loop.

Since I haven’t worked with Delphi in years, I can’t test it, 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;
  • Thanks Marcos, I could see the error. It really doesn’t make sense to compare upper case to lower case.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.