Skip to content

Instantly share code, notes, and snippets.

@zaidw21
Last active February 12, 2020 08:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zaidw21/14525a6dfcf0762e6d09e1e1b5747f53 to your computer and use it in GitHub Desktop.
Save zaidw21/14525a6dfcf0762e6d09e1e1b5747f53 to your computer and use it in GitHub Desktop.
confluence
public class Solution {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int len = s.nextInt();
s.nextLine();
String str = s.nextLine();
int shift = s.nextInt();
char sarr[] = str.toCharArray();
for (int i = 0; i < sarr.length; i++) {
sarr[i] = cryptIt(sarr[i], shift);
}
System.out.println(new String(sarr));
}
public static char cryptIt(char c, int shift) {
if (!Character.isAlphabetic(c))
return c;
char base = 'A';
if (c >= 'a')
base = 'a';
return (char) (((c - base + shift) % 26) + base);
}
}
@shivijuhi
Copy link

static String caesarCipher(String s, int k) {

	String newString = "";
	String alphabet = "abcdefghijklmnopqrstuvwxyz";
	String capsAlpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	char c;
	for (int i = 0; i < s.length()-1; i++) {

		c = s.charAt(i);
		int index = alphabet.indexOf(c);
		if(index == -1) {
			index = capsAlpha.indexOf(c);
		}
		index = index + k;
		newString = newString + alphabet.charAt(index);

	}
	return newString;

}

bas ek cheez nahi ho ri z k bad rotation waps a pe

@shivijuhi
Copy link

dkho isko apne pe aadha ho gaya

@shivijuhi
Copy link

static String caesarCipher(String s, int k) {

	String newString = "";
	String alphabet = "abcdefghijklmnopqrstuvwxyz";
	String capsAlpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	char c;
	for (int i = 0; i < s.length()-1; i++) {

		c = s.charAt(i);
		int index = alphabet.indexOf(c);
		
		if(index == -1) {
			index = capsAlpha.indexOf(c);
			index = index + k;
			newString = newString + capsAlpha.charAt(index);
		}else {
			index = index + k;
			newString = newString + alphabet.charAt(index);
		}
		

	}
	return newString;

}

@shivijuhi
Copy link

thoda sa change kiya

@shivijuhi
Copy link

static String caesarCipher(String s, int k) {

	String newString = "";
	String alphabet = "abcdefghijklmnopqrstuvwxyz";
	String capsAlpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	String regEx = "-,/><?';:}{[]";
	char c;
	for (int i = 0; i < s.length() - 1; i++) {

		c = s.charAt(i);

		int index = alphabet.indexOf(c);

		if (index > -1) {
			index = index + k;
			newString = newString + alphabet.charAt(index);
		} else if (regEx.indexOf(c) != -1) {
			newString = newString + s.charAt(i);
		} else {
			index = capsAlpha.indexOf(c);
			index = index + k;
			newString = newString + capsAlpha.charAt(index);
		}
	}
	return newString;

}

bas rotation bta do

@shivijuhi
Copy link

baki sb chal rha

@shivijuhi
Copy link

static String caesarCipher(String s, int k) {

	String newString = "";
	String alphabet = "abcdefghijklmnopqrstuvwxyz";
	String capsAlpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	String regEx = "-,/><?';:}{[]";
	char c;
	for (int i = 0; i < s.length(); i++) {

		c = s.charAt(i);

		int index = alphabet.indexOf(c);

		if (index > -1) {
			index = index + k;
			if(index>26) {
				index = index - 26;
			}
			newString = newString + alphabet.charAt(index);
		} else if (regEx.indexOf(c) != -1) {
			newString = newString + s.charAt(i);
		} else {
			index = capsAlpha.indexOf(c);
			index = index + k;
			
			if(index>26) {
				index = index - 26;
			}
			
			newString = newString + capsAlpha.charAt(index);
		}
	}
	return newString;

}

isko bas optimize kr do.. wo nahi aata.. repeatitive code likg=h re hm wo galat hai na

@shivijuhi
Copy link

kam kr rha ye but pura

@zaidw21
Copy link
Author

zaidw21 commented Feb 12, 2020

let me complete 2nd first, i will check this then.. till then u try to do whatver u want

@shivijuhi
Copy link

static String caesarCipher(String s, int k) {

	String newString = "";
	String alphabet = "abcdefghijklmnopqrstuvwxyz";
	char c;
	
	for (int i = 0; i < s.length(); i++) {
		boolean flag = false;
		c = s.charAt(i);
		int index = alphabet.indexOf(c);
		if (index == -1) {
			index = alphabet.toUpperCase().indexOf(c);
			flag = true;
			if (index == -1) {
				newString = newString + s.charAt(i);
				continue;
			}
		}
		index = index + k;
		if (index > 26) {
			index = index - 26;
		}
		
		if(flag) {
			newString = newString + alphabet.toUpperCase().charAt(index);
		}else {
			newString = newString + alphabet.charAt(index);
		}
	}
	return newString;

}

ekdum otimize kr diya..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment