1
votes

What I want to do is read the characters in succession and count the number of characters in uppercase, lower case, numbers, and space. (we're told not to use strings).

My question is, why is this code not working? isn't it supposed to read the 1st character and then read the 2nd character until the last character of the input?

How can I make a loop that reads the 1st character in the input then read the character after that and so on...when it sees the end of line it should stop the loop.

program Contadordecaracteres;

VAR
  minusculas,mayusculas,numeros,espacios:integer;
  c:char;


begin
  minusculas:= 0;
  mayusculas:= 0;
  numeros:= 0;
  espacios:= 0;
  writeln('Escriba algo');
  while not EoLn do
  begin
  read(c);

  case c of
       ' ': espacios:= espacios+1;
       '0'..'9': numeros:= numeros+1;
       'A'..'Z': mayusculas:= mayusculas+1;
       'a'..'z': minusculas:= minusculas+1;

  end; //end of case
  end; // end of while

  writeln('Mayusculas: ', mayusculas);
  writeln('Minusculas: ', minusculas);
  writeln('Numeros: ', numeros);
  writeln('Espacios: ', espacios);
  readln;


end.
1
For an exercise like this, using traditional Pascal file io, it is tyically far more instructive to write the program so that it reads the input from a text file into a string variable than to read the input a character at a time from the keyboard. This will instroduce you to string-manipulation, which is a basic required skill. Google yourself a tutorial that suits your level. - MartynA
Thank you for your edit. I removed my downvote and all previous comments as it is now clear what you ask. And finally also answered your question. If my answer is correct you can mark it as such by clicking the tick mark beside my answer. - Tom Brunberg

1 Answers

0
votes

How can I make a loop that reads the 1st character in the input then read the character after that and so on...when it sees the end of line it should stop the loop.

With the exception of one detail, your program does essentially what you want it to do. The one detail that doesn't work as you expect, is that, when you press Enter (after hitting various keys before that), the program runs quickly through the writeln()s and the readln and then terminates. I guess you expected the program to stop at this readln; line.

With the while not EoLn do statement you require the user to press Enter to see the summary. That's ok.

The while not EoLn do sees the CR LF (Carriage Return Line Feed) produced by that key press and is satisfied to exit the loop, but doesn't remove the CR LF from the input buffer.

When the readln (after all writelns) is performed, it sees the CR LF in the input buffer, reads it from the buffer and continues, which leads to end of program.

The cure is to add one more readln; after the one you already have. Then the program will stop until you again press Enter.