Domanda

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?.

È stato utile?

Soluzione

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 :)

Altri suggerimenti

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.

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

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top