Jump to content

I've confused on how I would be able to reverse a string but still be able to keep the form. one example I have is, "*|| *| |||" but it would have to look like this when reversed "||| *| *||".

 

I currently have a way to reverse it using for loops but I can't find a way to keep the form.

 

Link to comment
https://linustechtips.com/topic/748676-reversing-string-but-keeping-form-java/
Share on other sites

Link to post
Share on other sites

A simple way would be to create an array list and add each new word (look for spaces) as a new element in the list. Then just print out the list backwards. 

PSU Tier List | CoC

Gaming Build | FreeNAS Server

Spoiler

i5-4690k || Seidon 240m || GTX780 ACX || MSI Z97s SLI Plus || 8GB 2400mhz || 250GB 840 Evo || 1TB WD Blue || H440 (Black/Blue) || Windows 10 Pro || Dell P2414H & BenQ XL2411Z || Ducky Shine Mini || Logitech G502 Proteus Core

Spoiler

FreeNAS 9.3 - Stable || Xeon E3 1230v2 || Supermicro X9SCM-F || 32GB Crucial ECC DDR3 || 3x4TB WD Red (JBOD) || SYBA SI-PEX40064 sata controller || Corsair CX500m || NZXT Source 210.

Link to post
Share on other sites

You could parse it into a char-array and reverse it using a for-loop, where you iterate from the highest index to 0, and simply add the chars to your new string.

Something like this: (Didnt test it, so no promises this snipplet will work).

		String s = "abcd";
		String newString = null;
		char[] c = s.toCharArray();
		for (int i = c.length-1; i >= 0; i--) {
			newString += c[i];
		}

 

EDIT: Nevermind. I didnt realise you wanted to reverse the order of the words, and not the characters. Either way, you can use this solution if you use s.split(" ") into a String-array instead of s.toCharArrary()

Running Arch with i3-gaps on a Thinkpad X1 Extreme
Data Science Postgrad

 

Link to post
Share on other sites

1 minute ago, Organized said:

I think you guys got him wrong. Look at his example :)

 

You have to split the String at the spaces, so you get a String-array. Then you can use @Claryn's loop to reverse it.

I think I ninja'd you with my edit :D

Running Arch with i3-gaps on a Thinkpad X1 Extreme
Data Science Postgrad

 

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×