Page 1 of 1 |
|
Posted: Tue, 13th Nov 2007 19:11 Post subject: Java Help ! |
|
 |
Hello,
How can i change a character like 'a' into 'b' ?
example:
if i type "Java" as input. It must change as output into: "Kbwb".
Like an cryptography.
Please can somebody help me ? I'm stuck with this problem.
Greetings
|
|
Back to top |
|
 |
[sYn]
[Moderator] Elitist
Posts: 8374
|
Posted: Tue, 13th Nov 2007 19:44 Post subject: |
|
 |
The easiest way is to read the text into an array.. create a for loop that runs through every element of that array filled with an IF statement for every letter of the alphabet ..
eg.
for i = 26..etc..
If array[i] = a then
array[i] = b;
else if array[i] = b then
array[i] = c;
else if... etc...
|
|
Back to top |
|
 |
|
Posted: Tue, 13th Nov 2007 19:50 Post subject: |
|
 |
Don't know if this is even possible in java but wouldn't it be easier to use the character code for each letter and just +1 to it and convert back?
|
|
Back to top |
|
 |
|
Posted: Tue, 13th Nov 2007 19:51 Post subject: |
|
 |
That is possible but i dont know how.
|
|
Back to top |
|
 |
|
Posted: Wed, 14th Nov 2007 00:06 Post subject: |
|
 |
Code: |
StringBuilder aStringBuilder = new StringBuilder("Java");
for (int i = 0; i < aStringBuilder.length(); i++) {
aStringBuilder.setCharAt(i,(char)(aStringBuilder.charAt(i)+1));
}
System.out.println(aStringBuilder.toString());
|
|
|
Back to top |
|
 |
|
Posted: Wed, 14th Nov 2007 15:50 Post subject: |
|
 |
If you want to do it the LEET programmer way, which makes you irresistible to chicks by the way, you might also try it this way
Code: |
import java.io.ByteArrayInputStream;
public class CharSwap {
public static void main(String[] args) {
if (args.length < 1) {
return;
}
ByteArrayInputStream in = new ByteArrayInputStream( args[0].getBytes() );
StringBuffer out = new StringBuffer();
byte inChar;
while ( (inChar = (byte) in.read() ) != -1) {
out.append( new String( new byte[] { (byte) (inChar + 1) } ) );
}
System.out.println(out);
}
}
|
rgds
Sabalasa
|
|
Back to top |
|
 |
Page 1 of 1 |
All times are GMT + 1 Hour |