aspose file tools*
The moose likes Beginning Java and the fly likes String reconstruction Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "String reconstruction" Watch "String reconstruction" New topic
Author

String reconstruction

Hongli Li
Ranch Hand

Joined: Oct 29, 2006
Posts: 124
Hi, folks

Need some ideas on how you will do this:

receive a string, and then chop it into 3 pieces, use any of the 2 resultant strings, it should be able to restore the original string.

Thanks


Do you know why this cup is useful? Because it is empty.
Jess Ryan
Greenhorn

Joined: Feb 16, 2008
Posts: 21
Well, the most powerful way of approaching this (in my opinion) would be to use java.util.regex.Pattern and java.util.regex.Matcher. With these classes, you can create patterns to search and/or parse a string just about any way you can think of.

Another approach would be to use java.util.StringTokenizer, but this class is going out of style and its use is discouraged in new code.
Hongli Li
Ranch Hand

Joined: Oct 29, 2006
Posts: 124
Originally posted by Jess Finley:
Well, the most powerful way of approaching this (in my opinion) would be to use java.util.regex.Pattern and java.util.regex.Matcher. With these classes, you can create patterns to search and/or parse a string just about any way you can think of.

Another approach would be to use java.util.StringTokenizer, but this class is going out of style and its use is discouraged in new code.

Hi Jess, thanks for your reply, but there is no pattern, I need to slice the string 3 parts evenly, and then do some "Magic" so that with any of the 2 parts out of 3, I will be able to restore the original string,
Jess Ryan
Greenhorn

Joined: Feb 16, 2008
Posts: 21
Huh, well, I don't understand the problem... specifically the "Magic" I don't see how you can leave 1/3 of the string behind and still have the whole string. But, you can find out the input string's length (String.length()) and divide that in to three parts, for starters...
Nicholas Jordan
Ranch Hand

Joined: Sep 17, 2006
Posts: 1282
Start with an int length = string.length(), do some arithmetic then use substring(int,int) method of the string class to allocate three StringBuffers, which is a class right next to String in the documentaion.

StringBuffer class has ways of doing what you want. String class is resistant to chop it into 3 pieces
[ March 01, 2008: Message edited by: Nicholas Jordan ]

"The differential equations that describe dynamic interactions of power generators are similar to that of the gravitational interplay among celestial bodies, which is chaotic in nature."
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[Nick]: StringBuffer class has ways of doing what you want. String class is resistant to chop it into 3 pieces

Not really. It's pretty easy to create three pieces using the substring() method, with no StringBuffers anywhere.

The real problem here is the stuff Jess has been asking about: how can we reconstruct the original string using only 2 of the three pieces?

Hongli Li, let's take an example. If the original string is "abcdefghi", we can split that into three parts, "abc", "def", "ghi". Is that what you mean? OK, then if we take just two of those, "abc" and "def", how can we reconstruct the original string? If we don't know anything about "ghi", how can we recreate that data?

I have the idea that this may actually be a much more complex problem, where we don't really split the original string, but create three different smaller strings (not substrings) from which it is possible to reconstruct the original string. Perhaps the goal is to find the smallest set of strings for which this is possible, or to ensure that it is not possible to recover the original string from only 1 of the three strings, but it is always possible using any 2 of the three strings. I think you would really need to get a better idea of what the requirements really are before spending too much time on it. The problem I'm thinking of would definitely not be a beginner's problem though. It's probably more of a cryptography problem.
[ March 01, 2008: Message edited by: Jim Yingst ]

"I'm not back." - Bill Harding, Twister
Nicholas Jordan
Ranch Hand

Joined: Sep 17, 2006
Posts: 1282
[Jim:]It's probably more of a cryptography problem.
Yes, that is why I suggested StringBuffer - the one time I looked at it the intent seemed ( from memory ) to derive from the need to manipulate Strings. I have been studying crypto for several months now and that is why I tried to simplifiy it. Crypto and beginner area do not mix, I have bruises to show for it. The words of original poster: need to slice the string 3 parts evenly, and then do some "Magic" so that with any of the 2 parts out of 3, I will be able to restore the original string so if is XOR I imagine Hongli Li could byte[] array = string.getBytes(); which yields a byte array and is what they seem to use in encipherment when they want to do magic. Since our honored master (has) the idea that this may actually be a much more complex problem ..., I suggest we invite the original poster to Security where complex work is much more common and well received.
Hongli Li
Ranch Hand

Joined: Oct 29, 2006
Posts: 124
this is a vintage thread, back to the days I even couldn't elaborate myself well, it's a cryptography problem and I solved it with Shamir's Secret Sharing.
Thanks guys even thought it's too late to say it.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36592
    
  16
Thank you for the solution. We have a saying in Britain: "better late than never."
Hongli Li
Ranch Hand

Joined: Oct 29, 2006
Posts: 124
Campbell Ritchie wrote:Thank you for the solution. We have a saying in Britain: "better late than never."


Yeah, nice to know the precious saying and It feels good to share the knowledge.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String reconstruction
 
Similar Threads
Tech Word Game
date format with MS Access database
How to convert byte array into string array?
WA #1.....word association
ResultSet to Array