1
String source = "WEDGEZ"
char letter = source.charAt(i);
shift=5;
for (int i=0;i<source.length();i++){
if (source.charAt(i) >=65 && source.charAt(i) <=90 )
  letterMix =(char)(('D' + (letter - 'D' + shift) % 26));
}

Ok what I'm trying to do is take the string WEDGEZ, and shift each letter by 5, so W becomes B and E becomes J, etc. However I feel like there is some inconsistency with the numbers I'm using.

For the if statement, I'm using ASCII values, and for the letterMix= statement, I'm using the numbers from 1-26 (I think). Well actually, the question is about that too:

What does (char)(('D' + (letter - 'D' + shift) % 26)); return anyway? It returns a char right, but converted from an int. I found that statement online somewhere I didn't compose it entirely myself so what exactly does that statement return.

The general problem with this code is that for W it returns '/' and for Z it returns _, which I'm guessing means it's using the ASCII values. I really dont know how to approach this.

Edit: New code

    for (int i=0;i<source.length();i++)
        {
        char letter = source.charAt(i);
        letterMix=source.charAt(i);
        if (source.charAt(i) >=65 && source.charAt(i) <=90 ){
            letterMix=(char)('A' + (  ( (letter - 'A') + input ) % 26));
            }
        }
Ash
  • 672
  • 6
  • 21
newtojava
  • 15
  • 1
  • 4

2 Answers2

5

Well I'm not sure if this homework, so i'll be stingy with the Code.

You're Writing a Caesar Cipher with a shift of 5.

To address your Z -> _ problem...I'm Assuming you want all the letters to be changed into encoded letters (and not weird Symbols). The problem is ASCII values of A-Z lie between 65 and 90. When coding Z (for eg), you end up adding 5 to it, which gives u the value 95 (_).

What you need to do is Wrap around the available alphabets. First isolate, the relative position of the character in the alphabets (ie A = 0, B = 1 ...) You Need to subtract 65 (which is ASCII of A. Add your Shift and then apply modulus 26. This will cause your value to wrap around.

eg, it your encoding Z, (ASCII=90), so relative position is 25 (= 90 - 65). now, 25 + 5 = 30, but you need the value to be within 26. so you take modulus 26 so 30 % 26 is 4 which is E.

So here it is

char letter = message(i);
int relativePosition = letter - 'A'; // 0-25
int encode = (relativePosition + shift) % 26
char encodedChar = encode + 'A' // convert it back to ASCII.

So in one line,

char encodedChar = 'A' + (  ( (letter - 'A') + shift ) % 26)

Note, This will work only for upper case, if your planning to use lower case, you'll need some extra processing.

You can use Character.isUpperCase() to check for upper case.

st0le
  • 33,375
  • 8
  • 89
  • 89
  • Thanks so much. That really helped! It fixed a lot of issues, but I'm still having an issue when I use negative shift values. For example, if I use -4 and I'm at the letter C, I want to get Y, but instead I get some punctuation. I ran the code manually on paper to see what I would get, and it should be working correctly. I dont know why it isnt. See edit for the code that I'm using. – newtojava Sep 24 '10 at 17:40
  • @newtojava, That's coz in java, the `mod` operator gives out negative values as well...a simple fix, would be to add `if(shift < 0) shift += 26;` before decrypting...and don't subtract it this time, use the same `+` sign. – st0le Sep 24 '10 at 20:00
0

You can try this code for convert ASCII values to Char

class Ascii {

public static void main(String[] args) {

    Scanner sc=new Scanner(System.in);

    char ch=sc.next().charAt(0);

    if(ch==' ') {

    int in=ch;

    System.out.println(in);

    }

}

}

  • 1
    Welcome to Stack Overflow! Please don't answer just with source code. Try to provide a nice description about how your solution works. See: [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer). Thanks – sɐunıɔןɐqɐp Jul 12 '18 at 07:45