[Pascal] Counting letters in a row without arrays

AI Thread Summary
The discussion revolves around creating a program to count consecutive letters in a word and identify the most repeated letter in a row, without using arrays, strings, or subfunctions. The initial approach involved using a while loop to read characters until a return key is pressed, but challenges arose in managing the input and storing results without arrays. Attempts included defining multiple character variables and an integer to track counts, but issues persisted in printing results only after the entire input was processed. A suggestion was made to consider recursion, although the original poster indicated a need to process the entire input before outputting results. The focus remains on finding a solution that adheres to the exercise's constraints while effectively counting and displaying the required letter frequencies.
trash
Messages
13
Reaction score
0
I'm working with this excercise: make a program that gives the number of consecutive letters of a word and the most repeated letter in a row -example: if i enter aaafdseergftth and i press return the program should return a = 3, e=2, t=2 and a=3-.

I've come up with a couple of "solutions", like define a string and then use an array to get the characters and compare then with a while loop, but here's the issue: i can't use arrays, string, sunbfunctions to solve this excersise, and explicitely says i have to look for another solution.

Now here is my second idea without using strings or arrays: Define an unknown amounts of char variables and enter each one until Intro is entered with a while loop like While not (Eoln) do (...). And that's the only thing i can come up right now to solve it, but when i was looking for a way to define an unknown amount of variables i found nothing but a solution with an array that i should resize to enter new variables.

The next attempt: i defined tree variables char and one int. Then i defined a While Not (Eoln) that checks every letter entered and compare it with the one before, that would almost solve the excercise. The issue is that every time i enter a letter the while loop will be triggered, then after the loop -after i enter return- i could only print the character of the last of consecutive row of letters -the while is needed because i want the result after i entered all the letters-. If i try with the example above i get: t = 2.

Any ideas?
 
Technology news on Phys.org
trash said:
The next attempt: i defined tree variables char and one int. Then i defined a While Not (Eoln) that checks every letter entered and compare it with the one before, that would almost solve the excercise.

That's a good idea.

The issue is that every time i enter a letter the while loop will be triggered, then after the loop -after i enter return- i could only print the character of the last of consecutive row of letters

When you have processed the first 4 characters "aaaf", you know enough to print out "a = 3". You don't have to process the whole string before you print out something.
 
AlephZero said:
When you have processed the first 4 characters "aaaf", you know enough to print out "a = 3". You don't have to process the whole string before you print out something.
Thanks for answring AlephZero.
I have to enter the whole thing before i get the answer, that's what the excercise ask for.

If i don't process the whole thing wouldn't it go like this?
[Enter a]
Nothing happens
[Enter a]
Nothing happens
[Enter a]
Nothing happens
[Enter f]
prints a=3

I would like something like:
[Enter a]
[Enter a]
[Enter a]
[Enter b]
[Enter b]
[Enter f]
[Enter Return]
prints a=3, b=2
 
trash said:
I have to enter the whole thing before i get the answer, that's what the excercise ask for.

Hint: Can you use or have you been introduced to "recursion" yet?

If not then ignore this.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top