Caesar Cipher


This is a companion discussion topic for the original entry at https://toph.co/p/caesar-cipher

import java.util.Scanner;

public class solution {

public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    int n = scan.nextInt();
    scan.nextLine();
    String ss = scan.nextLine();

    char[] arr = ss.toCharArray();

    for (int i = 0; i < arr.length; i++) {
        if (arr[i] == ' ') {
            System.out.print(arr[i]);
        } else {
            int num = (int) arr[i];
            if (num - n < 97) {
                int re = (num - n) + 26;
                System.out.print((char)re);
                
            } else {
                System.out.print((char) (num-2));
            }
        }
    }
    System.out.println();
}

}
what’s wrong with that code
it shows wrong ans in 2nd testcase

what is wrong in my code?
#include<bits/stdc++.h>
using namespace std;
int main(){
int i=0,n;
cin>>n;
char str[100],st[100];
fflush(stdin);
cin.getline(str,100);
//upercase to lowercase convert
int k=0;
i=0;
while(str[i]!=’\0’){
if((int)str[i]<90){
st[k++]=str[i]+32;
}else{
st[k++]=str[i];
}
i++;
}
st[k]=’\0’;
//end upercase
i=0;
int j=0;
char s[100];
while(st[i]!=’\0’){
if(st[i]==’@’){
s[j]=’ ';
}else{
if((int)st[i]-n>96){
s[j]=st[i]-n;
}else{
s[j]=st[i]+26-n;
}

    }
    i++;
    j++;
}
s[j]='\0';
int len=j;
for(int i=0;i<len;i++){
    cout<<s[i];
}
return 0;

}

What’s wrong in this Code??
n = int(input())
element_of_n = list(map(str, input().split()))
new_element = []
for i in range(len(element_of_n)):
tem = []
for j in range(len(element_of_n[i])):
if element_of_n[i][j] == β€˜a’:
tem.append(β€˜y’)
elif element_of_n[i][j] == β€˜b’:
tem.append(β€˜z’)
else:
o_t = ord(element_of_n[i][j])
tem.append(chr(o_t-2))
new_element.append(’’.join(tem))
print(*new_element)

You are appending β€˜y’ if β€˜a’ is equal to element_of_n[i][i]. So you are only accounting for
2 as β€˜n’.

Whats the problem?


#include<set>
#include<iostream>
#include<vector>
#include<algorithm>
#include<stack>
#include<climits>
#include<cstring>
#include<cmath>
#include<unordered_map>
#include<sstream>
#include<cmath>
#include<iomanip>
#include<numeric>
using namespace std;

string is(string n){
    string al="YZABCDEFGHIJKLMNOPQRSTUVWX";
    for(int i=0;i<n.size();i++){
        if(isalpha(n[i])) n[i]=tolower(al[n[i]-'a']);
    }
    return n;
}
int main(){
	ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n;
    cin>>n;
    cin.ignore();
    string s;
    getline(cin,s);
    cout<<is(s);
}

The problem is, in your solution you are predicting that the key N will always be 2. What if the key changes? Will your solution be working then? Try fixing this issue and you can get an AC.

1 Like

what is the problem with my code

#include
#include
#include
using namespace std;

int main(){
int n;cin>>n;
for(int i=0; i<n; i++){
string s;cin>>s;
for(int j=0;j<s.size();j++){
if(s[j]==β€˜a’){
cout<<β€˜y’;
}
else if(s[j]==β€˜b’){
cout<<β€˜z’;
}
else{
if(isalpha(s[j])){
char ch=s[j]-2;
cout<<ch;
}
else{
cout<<s[j];
}
}
}
if(i!=n-1){
cout<<" ";
}
}
}

code
#python 3.8
A = input()
B = input()
letter = "abcdefghijklmnopqrstuvwxyz"
new=""
a = 0
b = len(B)-1
while a <= b:
	if B[a] == " ":
		new += " "
	else:
		g = letter.index(B[a])
		g -= 2
		if g < 0:
			g += 26
		new += letter[g]
	a += 1
print(new)
2
c
>> a
3
c
>> a

why is this happening