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:
Do what you did the first time, but reverse the string at the start :p

Haven't got Java to hand, but something like this is what you are probably after:

Code:
            for (int row = word.length; row >= 0; row--)
            {
                for (int count = row; count >= 0; count--)
                {
                    System.out.print(word[count]);
                }
                System.out.println();
            }
 
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
 
Not got an IDE installed to actually think this through... but:

Code:
        String output = "";

        for (int i = word.length; i > 0; i--) {
            output = word[i - 1] + output;
            System.out.println(output);
        }

Seem right?

Edit: Derp! Got >/< mixed up. Fixed now!
 
Last edited:
Realised I've got VS installed. My previous in C# :

Code:
            Char[] word = { 'H', 'e', 'l', 'l', 'o', };

            String output = "";

            for (int i = word.Length; i > 0; i--) {
                output = word[i - 1] +  output;
	        Console.WriteLine(output);
            }
            System.Console.ReadKey();

=

Code:
o
lo
llo
ello
Hello

:)


Edit: :o Just realised how late to the party I am. :( Changed them round to have the solution you've been missing. :)
 
Last edited:
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 :).
 
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

I graduated recently from Uni and started my first job doing java programing just a few months ago. If they taught it all in Uni you'd be there for decades. :p I seem to learn something new and useful every day at work at the moment. :)
 
Back
Top Bottom