1
\$\begingroup\$

I wrote this program that counts the number of letters in random.txt file in the Pascal programming language.
It doesn't matter if the letters are capitalized. Other characters are ignored. When it finishes it writes every letter in a single line and then after it the number of appearences of that letter in the text file.

program letters;
var 
random:text;
a1,b1,c1,d1,e1,f1,g1,h1,i1,j1,k1,l1,m1,n1,o1,p1,q1,r1,s1,t1,u1,v1,w1,x1,y1,z1:integer;

procedure counting(random:text; var a1,b1,c1,d1,e1,f1,g1,h1,i1,j1,k1,l1,m1,n1,o1,p1,q1,r1,s1,t1,u1,v1,w1,x1,y1,z1:integer);
var 
c:char;
begin
a1:=0;
b1:=0;
c1:=0;
d1:=0;
e1:=0;
f1:=0;
g1:=0;
h1:=0;
i1:=0;
j1:=0;
k1:=0;
l1:=0;
m1:=0;
n1:=0;
o1:=0;
p1:=0;
q1:=0;
r1:=0;
s1:=0;
t1:=0;
u1:=0;
v1:=0;
w1:=0;
x1:=0;
y1:=0;
z1:=0;
while not eof(random) do
while not eoln do
begin
if (c='A') or (c='a') then a1:=a1+1;
if (c='B') or (c='b') then b1:=b1+1;
if (c='C') or (c='c') then c1:=c1+1;
if (c='D') or (c='d') then d1:=d1+1;
if (c='E') or (c='e') then e1:=e1+1;
if (c='F') or (c='f') then f1:=f1+1;
if (c='G') or (c='g') then g1:=g1+1;
if (c='H') or (c='h') then h1:=h1+1;
if (c='I') or (c='i') then i1:=i1+1;
if (c='J') or (c='j') then j1:=j1+1;
if (c='K') or (c='k') then k1:=k1+1;
if (c='L') or (c='l') then l1:=l1+1;
if (c='M') or (c='m') then m1:=m1+1;
if (c='N') or (c='n') then n1:=n1+1;
if (c='O') or (c='o') then o1:=o1+1;
if (c='P') or (c='p') then p1:=p1+1;
if (c='Q') or (c='q') then q1:=q1+1;
if (c='R') or (c='r') then r1:=r1+1;
if (c='S') or (c='s') then s1:=s1+1;
if (c='T') or (c='t') then t1:=t1+1;
if (c='U') or (c='u') then u1:=u1+1;
if (c='V') or (c='v') then v1:=v1+1;
if (c='W') or (c='w') then w1:=w1+1;
if (c='X') or (c='x') then x1:=x1+1;
if (c='Y') or (c='y') then y1:=y1+1;
if (c='Z') or (c='z') then z1:=z1+1;
end;
close(random);
end;

procedure result(a1,b1,c1,d1,e1,f1,g1,h1,i1,j1,k1,l1,m1,n1,o1,p1,q1,r1,s1,t1,u1,v1,w1,x1,y1,z1:integer);
begin
writeln('A','',a1);
writeln('B','',b1);
writeln('C','',c1);
writeln('D','',d1);
writeln('E','',e1);
writeln('F','',f1);
writeln('G','',g1);
writeln('H','',h1);
writeln('I','',i1);
writeln('J','',j1);
writeln('K','',k1);
writeln('L','',l1);
writeln('M','',m1);
writeln('N','',n1);
writeln('O','',o1);
writeln('P','',p1);
writeln('Q','',q1);
writeln('R','',r1);
writeln('S','',s1);
writeln('T','',t1);
writeln('U','',u1);
writeln('V','',v1);
writeln('W','',w1);
writeln('X','',x1);
writeln('Y','',y1);
writeln('Z','',z1);
end;

begin
    assign(random,'random.txt');
    reset(random);
    counting(random,a1,b1,c1,d1,e1,f1,g1,h1,i1,j1,k1,l1,m1,n1,o1,p1,q1,r1,s1,t1,u1,v1,w1,x1,y1,z1);
    result(a1,b1,c1,d1,e1,f1,g1,h1,i1,j1,k1,l1,m1,n1,o1,p1,q1,r1,s1,t1,u1,v1,w1,x1,y1,z1);
    readln();
end.

I'm a beginner and I suppose this can be done a lot better and simpler without having to use 26 variables. How could I improve my code to make it work better? Could you give me some tips?

\$\endgroup\$
  • \$\begingroup\$ Did you consider to use an array? You will drop 80% of your code \$\endgroup\$ – Adriano Repetti Jul 6 '17 at 17:15
  • \$\begingroup\$ Yes, but I didn't know how to use it in this situation. Could you tip me how? @AdrianoRepetti \$\endgroup\$ – Plexus Jul 6 '17 at 18:02
  • \$\begingroup\$ I wrote my last line in pascal almost 25 years ago, I do not think I will write decent code. Outline: keep an array of 24 elements. Check if each char is c >= 'A' and c <= 'Z' and in case subtract 'A' to get index of array. Increment item at that index by 1. If your library does not have a tolower() function then repeat for a. Then use proper indentation and meaningful names (random as parameter name? Why?) \$\endgroup\$ – Adriano Repetti Jul 6 '17 at 18:51
  • \$\begingroup\$ " in case subtract 'A' to get index of array." - I didn't quite get this part. Regarding random as name, it's because I'm not a native english speaker, I had a more meaningful name in my language but I changed it so I could post it here. @AdrianoRepetti \$\endgroup\$ – Plexus Jul 6 '17 at 19:03
  • \$\begingroup\$ rosettacode.org/wiki/Letter_frequency#Pascal but in your case you need few lines more because calculation is case insensitive and you want to consider only Latin characters (subtract A means subtract its ASCII value, 65) \$\endgroup\$ – Adriano Repetti Jul 6 '17 at 19:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.