Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
52
Task/Caesar-cipher/C/caesar-cipher.c
Normal file
52
Task/Caesar-cipher/C/caesar-cipher.c
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
|
||||
#define caesar(x) rot(13, x)
|
||||
#define decaesar(x) rot(13, x)
|
||||
#define decrypt_rot(x, y) rot((26-x), y)
|
||||
|
||||
void rot(int c, char *str)
|
||||
{
|
||||
int l = strlen(str);
|
||||
|
||||
const char* alpha_low = "abcdefghijklmnopqrstuvwxyz";
|
||||
|
||||
const char* alpha_high = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||||
|
||||
|
||||
char subst; /* substitution character */
|
||||
int idx; /* index */
|
||||
|
||||
int i; /* loop var */
|
||||
|
||||
for (i = 0; i < l; i++) /* for each letter in string */
|
||||
{
|
||||
if( 0 == isalpha(str[i]) ) continue; /* not alphabet character */
|
||||
|
||||
idx = (int) (tolower(str[i]) - 'a') + c) % 26; /* compute index */
|
||||
|
||||
if( isupper(str[i]) )
|
||||
subst = alpha_high[idx];
|
||||
else
|
||||
subst = alpha_low[idx];
|
||||
|
||||
str[i] = subst;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
char str[] = "This is a top secret text message!";
|
||||
|
||||
printf("Original: %s\n", str);
|
||||
caesar(str);
|
||||
printf("Encrypted: %s\n", str);
|
||||
decaesar(str);
|
||||
printf("Decrypted: %s\n", str);
|
||||
|
||||
return 0;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue