[Pascal] Counting letters in a row without arrays

In summary: If so then try this: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.If you are defining a string you can use an array to do the same thing.
  • #1
14
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
  • #2
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.
 
  • #3
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
 
  • #4
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.
 
  • #5


I would suggest considering alternative data structures to solve this exercise without using arrays or strings. One possible solution could be using a linked list, where each node contains a character and a count of consecutive occurrences. This way, you can still keep track of the consecutive letters without using an array. Additionally, you could use a hash table to keep track of the most repeated letter in a row, updating the count each time a new consecutive letter is encountered. This would also eliminate the need for resizing an array. Overall, it may require some additional coding and logic, but it is possible to solve this exercise without using arrays or strings.
 

What is Pascal and how does it relate to counting letters in a row without arrays?

Pascal is a programming language that was developed in the 1970s by Niklaus Wirth. It is a high-level language that is known for its structured programming and strong data typing features. In this context, Pascal can be used to count letters in a row without using arrays, as it has built-in functions for string manipulation.

What is the benefit of counting letters in a row without using arrays?

Counting letters in a row without using arrays can be more efficient and faster than using arrays. This is because arrays require extra memory allocation and can be more complex to manage. By using built-in string functions in Pascal, the code can be simpler and more streamlined.

What is the process for counting letters in a row without arrays using Pascal?

The process for counting letters in a row without using arrays in Pascal involves several steps. First, the string is converted into an array of characters using the STRTOCHAR function. Then, a loop is used to iterate through the array and check each character using the CHR function. Finally, a counter variable is incremented every time a matching character is found. After the loop, the counter variable will contain the total number of occurrences of the specified letter in the string.

Can this method be used to count multiple letters in a row without using arrays?

Yes, this method can be used to count multiple letters in a row without using arrays. The loop can be modified to check for multiple letters by using the OR operator and the CHR function for each letter. For example, to count the occurrences of both 'a' and 'b' in a string, the loop could check for CHR(97) OR CHR(98) and increment the counter accordingly.

Are there any limitations to counting letters in a row without using arrays in Pascal?

While this method can be efficient and effective for counting letters in a row, it may not be suitable for more complex tasks. If the counting requires specific patterns or multiple conditions, using arrays may be a better approach. Additionally, some other programming languages may have more advanced functions or algorithms for counting letters in a row without using arrays.

Suggested for: [Pascal] Counting letters in a row without arrays

Replies
10
Views
675
Replies
4
Views
1K
Replies
4
Views
1K
Replies
8
Views
1K
Replies
7
Views
954
Replies
12
Views
1K
Replies
10
Views
1K
Back
Top