support lower case with new modulo
This commit is contained in:
parent
2f58a337f1
commit
c6b5f1bad7
20
src/crypt.c
20
src/crypt.c
@ -9,6 +9,8 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include "crypt.h"
|
#include "crypt.h"
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
|
||||||
// Which of the two below functions is faster (find out!)
|
// Which of the two below functions is faster (find out!)
|
||||||
inline int mod(int a, int b)
|
inline int mod(int a, int b)
|
||||||
@ -23,15 +25,22 @@ inline int fast_mod(int a, int b)
|
|||||||
return result >= 0 ? result : result + b;
|
return result >= 0 ? result : result + b;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Below two functions do NOT work for lower case letters (fix this!)
|
|
||||||
|
inline bool isupper(char c)
|
||||||
|
{
|
||||||
|
return c >= 'A' && c <= 'Z';
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void caesar_encrypt(char* str, int size, int key)
|
void caesar_encrypt(char* str, int size, int key)
|
||||||
{
|
{
|
||||||
for (int i = 0; i < size && str[i] != '\n'; i++)
|
for (int i = 0; i < size && str[i] != '\n'; i++)
|
||||||
if (str[i] != ' ')
|
if (str[i] != ' ')
|
||||||
{
|
{
|
||||||
str[i] += key - 65;
|
bool upper = isupper(str[i]);
|
||||||
|
str[i] += key - (upper ? 65 : 97);
|
||||||
str[i] %= 26;
|
str[i] %= 26;
|
||||||
str[i] += 65;
|
str[i] += (upper ? 65 : 97);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -41,8 +50,9 @@ void caesar_decrypt(char* str, int size, int key)
|
|||||||
for (int i = 0; i < size && str[i] != '\n'; i++)
|
for (int i = 0; i < size && str[i] != '\n'; i++)
|
||||||
if (str[i] != ' ')
|
if (str[i] != ' ')
|
||||||
{
|
{
|
||||||
str[i] = str[i] - key - 65;
|
bool upper = isupper(str[i]);
|
||||||
|
str[i] = str[i] - key - (upper ? 65 : 97);
|
||||||
str[i] = mod(str[i], 26);
|
str[i] = mod(str[i], 26);
|
||||||
str[i] += 65;
|
str[i] += (upper ? 65 : 97);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user