0

I'm working with this excercise: make a program that gives the number of consecutive letters of a word -ex: if i enter aaafdseergftth the program returns a = 3, e=2, t=2-.

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. How can i define an unknown amount of variables without using arrays? -is it even possible?-, and if can't be done, how could i get every character of a word without using arrays nor strings?.

0

3 Answers 3

1

The answer to your question is "No": you can't define an unknown amount of variables without an array, at least without using dirty-dirty hacks (and I'm still not sure if it becomes possible with them).

I'd suggest you think in this way: you don't really need the whole string, you just need to remember which character came before current one. That is the way to solve it :)

Sign up to request clarification or add additional context in comments.

2 Comments

What about node sort of structure, where each node has a reference to the next (or previous) sibling node ? Also there are many wrappers around a pure array in FPC. I'm quite lost what is the aim of this excercise to suggest something (there's an input string and an output must not be array, or something ?). [not voting down here]
Thanks haskile, but i still can't solve the whole excercise. Now 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.
1

It could be an hint to

  1. Use pointers/linked list like TLama said.
  2. Recursion, in some cases, also is a way of not predefining the number of variables.
  3. using of the dynamic (resizable) arrays that FPC provides.

I think the first is the most likely.

Comments

0

What about using a string?

If the input is ascii characters and the max occurrance of each letter is 94 you could store the count as a single ascii character and which letter it was as the index into the string. Then the whole thing is just string operations.

For example:

if a count of 0 is stored as a space. a count of 1 is stored as a !, a count of 33 is stored as an A. a count of 65 is stored as an a, a count of 94 is stored as a ~, etc (the ascii code minus 32)

And if the first character in the string represents the number of spaces and the second represents the number of ! and the 33rd character represents the number of A, and the 94th character represents the number of ~, etc (the ascii code minus 32)

Then an input of "a" would look have 93 spaces except the 65th character which could be encoded as ! to mean 1. And an input of "aabbab" would be all spaces except the 65th and 66th character which would be a # to mean 3.

Of course, if the max number of occurance is larger than 94 or they won't be ascii characters then this won't work. You could get around this by using two or more characters to represent each count but that quickly gets silly.

I didn't say it was a good idea and I would kill anyone that wrote real code like that but for a thought experiment where the goal is explicitely "look for another solution." then this fits that criteria.

Jerry

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.