String origString = "HELLO";
char[] origChars = origString.toCharArray();
char[] scrambledChars = new char[origChars.length];
int origLength = origChars.length;
for (int i=0;i<origLength;i++) {
if (i == 0) {
scrambledChars[i] = origChars[Math.min(origLen
} else if (i%2==0) {
scrambledChars[i] = origChars[Math.min(origLen
} else {
scrambledChars[i] = origChars[i-1];
}
}
String strScrambled = new String(scrambledChars);
System.out.println(strScra
Main Topics
Browse All Topics





by: mickeynoearPosted on 2002-12-05 at 10:28:29ID: 7538076
try using a StringBuffer object, 'charAt(int index)', 'setCharAt(int index, char ch)'.
lString.ch arAt(3));
,'U'); uffer));
eg. code:
StringBuffer originalString=new StringBuffer("Hello");
//<down>to get & print a char at specified location 2,
System.out.println(origina
0 1 2 3 4
//the output will be: 'l' for H e >l< l o
//to replace a char at a specified location 4 with a char 'U',
originalString.setCharAt(4
/*to print-->*/ System.out.println(StringB
//when u compile and run, the result: HellU
code ends.
this is only to get and replace the chars..
for how to scramble the characters, you create your own randomization technic okay, got it?
tips:
use a temp variable to store one of the chars when switching value.
e.g. :
a=1;
b=2;
//swapping
int c=a; //c is a temporary int variable..
a=b;
b=c;
if u don't do this, the value of a will be lost when u replace it with b. and when u want to store the value a to b, u will take a value similar to b instead. then, u will hav two vars with the same value.
hope that helps..