Monday, July 27, 2009

What is some JAVA code that will take a sentence and shorten it to 20 characters?

I need to write a java program that will shorten a string to 20 characters but by taking out random characters not just substring at 0,20.





For example: "This would be a sentence that is greater than 20 charcters"





I would like it to randomly make something like this: "TSWLDBESENTHATISGTAN"





So, it uses random letter until the string length is down to 20 characters and then all upper case the string.





Thanks for any help!

What is some JAVA code that will take a sentence and shorten it to 20 characters?
String s = "This would be a sentence that is greater than 20 characters";


int len = s.length();


while (len %26gt; 20) {


int i = (int)(Math.random() * len);


s = s.substring(0, i) + s.substring(i+1);


len--;


}


s = s.toUpperCase();

credit cards

No comments:

Post a Comment