java - How to decrypt this String -
given following string:
string s = "qba'g sbetrg gb qevax lbhe binygvar";
my desired output is:
don't forget drink ovaltine
my code:
public static string decrpyt(string s){ string ans = ""; (int = 0; i<s.length(); i++) { int x = s.charat(i); if (x <= 105) { char c = ((char) (x + 13)); ans += c; } else { char c = ((char) (x - 13)); ans += c; } } return ans; }
it returns:
don4t-forget-to-drink-_our-ovaltine
what need desired output?
you changing characters, not letters, has been mentioned in comment. consider range of characters letters need modify. declare x
char
don't have consider actual numeric ranges.
a-m => n-z action: += 13 a-m => n-z action: += 13 n-z => a-m action: -= 13 n-z => a-m action: -= 13 (all others) action: no change
this can expressed in 3 cases:
if ((x >= 'a' && x <= 'm') || (x >= 'a' && x <= 'm')) { // add 13 } else if ((x >= 'n' && x <= 'z') || (x >= 'n' && x <= 'z')){ // subtract 13 } else { // use }
Comments
Post a Comment