I think, step one for me would be to get the exact representation into some structure that you can easily manipulate. I agree, that a multidimensional array seems like the most reasonable model, as it closely fits the concept.
You need to read in each character into a new char[]
. You can do that using the code from this question. That's your step one. Getting each char
into an array and each array into a char[][]
.
Step two is the proximity checking. Let's look at some possibilities.
---1---1---11
---1-------11
So you know that you can access each member here. I can't think of a better way to check each element than something close to O(n^2)
. Not nice, but it's something for you to build on.
- Go through a line until you hit a value that is not white space.
- From this point, you're going to find everything on the same line as it. This means incrementing the current index until you either hit more white space, or the end of the array.
- Next is finding out what's below. You now know where the values start and finish. So you need to go back and check between your
start
index and your finish
index, each time going down the multi dimensional array.
- For each value you hit, increment some global pointer.
Following the example above, you'd go..
---1---1---11
^-----------------Found a character. Increment count.
---1---1---11
^----------------Only whitespace next to it. Stop looking.
---1---1---11
---1---1---11
^-----------------Found a character below. Increment count.
---1---1---11
---1---1---11
xxxxxxxxxxxxx
^-----------------We're at the end.
---1---1---11
^----------------There was no value after it. Search is complete.
Total Count = 2.
Then you'd move onto the next one. Now, for the sake of giving some complete examples, I'll do the last one to show you the algorithm at work.
---1---1---11
^---------Found a value that isn't whitespace. Increment count.
---1---1---11
^--------Found another value. Increment count.
---1---1---11
^-------At the end of the array. Stop looking.
---1---1---11
---1-------11
^---------Found a value. Increment count.
---1---1---11
---1-------11
xxxxxxxxxxxxx
^---------At the end. Stop looking.
---1---1---11
---1-------11
^--------Found a value. Increment count.
---1---1---11
---1---1---11
xxxxxxxxxxxxx
^--------At the end. Stop looking.
---1---1---11
^-------At the end of the top array. Finished search.
Total Count = 4.