0

Following is my code snippet:

 for (int i=0;i<(in.length());i++)
    {
        char e=cop.charAt(i);
        if (e==' ')
        {
            sav=sav+" ";
            continue;
        }
        int n=(int)e;
        if (n>=(91-ef))
        {
            //here is where the statement should be
            continue;
        }    
        n=n+ef;
        sav=sav+((char)n);
    }

in: String entered by the user

ef: encoding factor entered by the user

sav: in which I'm storing my modified String

Here what I need to do is input a string (word or sentence) in upper case and increase the ASCII value of each character of the string by an encoding factor entered by the user and print the respective character at the new ASCII value. Spaces will remain spaces and if the character should exceed 'Z', it'll start again from 'A'. It's the last part I'm having problem with. There should be one statement at the place I've commented in the above code but I can't figure out what it should be. Please help?

Here's an example what I need to do:

INPUT: tommy

OUTPUT: YTRRD (when the encoding factor is 5)

2
  • Hint: "tommy" is not upper case. Commented May 16, 2016 at 12:57
  • I know, I've converted it to upper case earlier in the code. Commented May 16, 2016 at 12:58

3 Answers 3

1

I would add first and then check it using using characters instead of raw numbers.

e = Character.toUpperCase(e);
e += 5;
if (e > 'Z')
   e -= 'Z' + 1 - 'A';
sav += e;

or using a constant.

static final int LETTERS_IN_ALPHABET = 'Z' + 1 - 'A';

// in code
e = Character.toUpperCase(e);
e += 5;
if (e > 'Z')
   e -= LETTERS_IN_ALPHABET;
sav += e;
Sign up to request clarification or add additional context in comments.

6 Comments

Please explain the 4th line?
@ShinjineeMaiti Suppose that e = 'Z' and ef = 1. This line takes the value of e back to 'A' instead of 'Z'+1.
It subtracts 26 without having to calculate how many letters there are. 'Z' + 1 becomes 'A', 'Z' + 2 becomes 'B' etc.
@TomBlodget Equations are not as readable to some people. English would be better.
|
0

In your code, this should work:

if (n>=(91-ef))
{
     n = 64 + (n+ef-90) -ef;
}    
  • There is no need for continue.
  • n+ef is the final "normal" value, eg: Y=89, so, on adding 5, it becomes 94.
  • -90 to check by how much it is greater than 90, i.e, 4 (in case of Y).
  • 64 + (n+ef-90) - Then add that number to 64, i.e. 64+4 = 68 = D
  • -ef as you add ef after that. The line after this piece of code is : n=n+ef;. That would make 68+4 = 72 = H. But you already have the needed char, i.e., D, so you subtract ef so it becomes 68-5 = 63. Then, in the next line, you add it to get 63+5 = 68 = D.

6 Comments

Didn't get the last bullet. Please re-explain?
@ShinjineeMaiti Did you get it now?
Now I get what you mean, but if you notice I've given a 'continue' after that so no need for the last bullet. Thanks though.
@ShinjineeMaiti You should not use continue. If you do, then the line sav=sav+((char)n); will not be executed, and the char will not be added to sav
I would directly convert that ASCII value to char in the same line itself
|
0

Follow these steps :

  1. e is a character in ASCII code. Convert it to a [0-25] value using e - 'A'
  2. Then add ef
  3. As the result can exceed 25 (i.e e > 'Z'), use a modulo 26
  4. Finally, add 'A' to the result in order to convert the value back to a ['A'-'Z'] value.

Code :

String in = "TOMMY";
String sav="";
int ef = 5;

for (int i=0;i<(in.length()); i++) {
  char e=in.charAt(i);
  if (e==' ')
  {
    sav=sav+" ";
  } else {
    sav += (char) ('A' + ((e - 'A' + ef) % 26));
  }
}

System.out.println(sav);

result :

YTRRD

  • Note : Using String+= is bad. use a StringBuilder instead.
  • Note : Replace (e - 'A' + ef) with (e - 'A' + ef + 26) if you want to enable negative values for ef.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.