Loops query...

Soldato
Joined
14 Dec 2005
Posts
12,488
Location
Bath
I've hit a brick wall :(. I've got a char array, called word, which funnily enough contains a word and I'm playing around doing different things with it.

One thing I've done is this:
Code:
        for (int row = 0; row < word.length; row++)
            {
            for (int count = 0; count <= row; count++)
                {
                System.out.print(word[count]);
                }
            System.out.println();
            }

So if for example the word in the array was Hello it'll output:
H
He
Hel
Hell
Hello



Another thing I've done is print it out a character at a time but starting from the end:
Code:
        for (int i = word.length - 1; i >= 0; i--)
            {
            System.out.println(word[i]);
            }
Using the same example of Hello that outputs:
o
l
l
e
H



What I'd like to do next is a combination of the two. I'd like it to output the last character, then the last two characters, etc. Maybe I'm just tired and lacking in coffee, but I just can't figure it out!
Using the same example the output should be:
o
ol
oll
olle
olleH


Can anyone show me the light? I've been fiddling around for a while now and can't get it to work :(. I've stuck them both into a java file here if anyone wants to play around, but TBH it's the logic of how to do it I'm lacking!

Cheers
 
Last edited:
Well, here's my solution in Python.

Me and Python have never got on :(. Might give Python another bash sometime though.


I've had a play around and managed to do moreless everything apart from the one I actually posted :D :confused:. Genius eh?!

Code:
        // print it with increasing number of letters per line
        for (int row = 0; row < word.length; row++) {
            for (int count = 0; count <= row; count++) {
                System.out.print(word[count]);
            }
            System.out.println();
        }

        
        System.out.println();

        
        for (int row = word.length - 1; row >= 0; row--) {
            for (int count = 0; count <= row; count++) {
                System.out.print(word[count]);
            }
            System.out.println();
        }

        
        System.out.println();

        
        for (int row = 0; row < word.length; row++) {
            for (int count = row; count >= 0; count--) {
                System.out.print(word[count]);
            }
            System.out.println();
        }


        System.out.println();


        for (int row = word.length - 1; row >= 0; row--) {
            for (int count = row; count >= 0; count--) {
                System.out.print(word[count]);
            }
            System.out.println();
        }


        System.out.println();


        // print it backwards one at a time
        for (int i = word.length - 1; i >= 0; i--) {
            System.out.println(word[i]);
        }
h
he
hel
hell
hello

hello
hell
hel
he
h

h
eh
leh
lleh
olleh

olleh
lleh
leh
eh
h

o
l
l
e
h
 
Edit: :o Just realised how late to the party I am. :( Changed them round to have the solution you've been missing. :)

That's great stuff, ta :).


TBH I'm surprised we didn't do any playing around like this in our Introduction to Programming classes at Uni! But that's what friday evenings are for eh!? :p


For anyone that may find it useful I've stuck them all in a java file here :).
 
Back
Top Bottom