You are viewing a single comment's thread. Return to all comments →
My Java Logic :
import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); String s = sc.next(); int k = sc.nextInt() % 26; String alphabets = "abcdefghijklmnopqrstuvwxyz"; char[] lowercase = alphabets.toCharArray(); char[] uppercase = alphabets.toUpperCase().toCharArray(); StringBuilder result = new StringBuilder(); for (char ch : s.toCharArray()) { if (Character.isUpperCase(ch)) { int x = 26 - (ch - 65); result.append((char) (x <= k ? uppercase[k - x] : ch + k)); } else if (Character.isLowerCase(ch)) { int x = 26 - (ch - 97); result.append((char) (x <= k ? lowercase[k - x] : ch + k)); } else result.append(ch); } System.out.println(result); } }
Seems like cookies are disabled on this browser, please enable them to open this website
Caesar Cipher
You are viewing a single comment's thread. Return to all comments →
My Java Logic :