I have two strings plainText and key. Now, if the plainText.length > key.length then i have to repeat the key until its the same length as that of plainText. here’s an example:
plainText = "helloworld"
key="foobar"
hence the key should be increased to “foobarfoob”.
One solution is to repeat the entire word and then remove the last characters until it reaches the same length as of plainText like “foobarfoobar” and then remove the characters “ar” .
Is there any better (or rather and Elegant) way to do this in java?
Your solution seems reasonable, other than the way you remove the last characters – don’t do it one at a time, just use substring or
StringBuilder.setLength():Or just change the append call to only get to the right size:
Personally I prefer the first – it’s simpler.