-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathszyfr-cezara.cpp
62 lines (49 loc) · 1.16 KB
/
szyfr-cezara.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
//
// szyfr-cezara.cpp
// 2-algorytmika/2-2-podstawowe-algorytmy/2-2-3-szyfr-cezara/
//
// Created by Jakub Piskorowski on 22/02/2022 wersja: 1.0
// Copyright © 2022 Jakub Piskorowski. All rights reserved.
// GitHub: https://github.com/PiskorowskiJakub/programming-course-cpp
//
// Przedstawienie szyfry Cezara
//
#include <iostream>
#include <cstring> // do strlen()
using namespace std;
void szyfruj(short klucz, char tab[]);
int main()
{
char tab[200];
short klucz;
cout<<"Podaj wyraz skladajacy sie z malych liter: ";
cin>>tab;
cout<<"Podaj klucz z przedzialu [-26..26]: ";
cin>>klucz;
szyfruj(klucz,tab); //szyfrowanie
cout<<"Po zaszyfrowaniu: "<<tab<<endl;
szyfruj(-klucz,tab); //deszyfrowanie
cout<<"Po rozszyfrowaniu: "<<tab<<endl;
return 0;
}
void szyfruj(short klucz, char tab[])
{
int dl = strlen(tab);
if(!(klucz >= -26 && klucz <= 26)) return;
if(klucz >= 0){
for(int i=0;i<dl;i++){
if(tab[i] + klucz <= 'z')
tab[i] += klucz;
else
tab[i] = tab[i] + klucz - 26;
}
}
else{
for(int i=0;i<dl;i++){
if(tab[i] + klucz >= 'a')
tab[i] += klucz;
else
tab[i] = tab[i] + klucz + 26;
}
}
}