58 lines
947 B
C
58 lines
947 B
C
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <stdio.h>
|
|
|
|
char shift(char c, int by)
|
|
{
|
|
int start = 0;
|
|
if(c >= 'a' && c <= 'z')
|
|
{
|
|
start = 'a';
|
|
}
|
|
else if(c >= 'A' && c <= 'Z')
|
|
{
|
|
start = 'A';
|
|
}
|
|
else return c;
|
|
c -= start;
|
|
c += by;
|
|
c %= 26;
|
|
return c + start;
|
|
}
|
|
|
|
char *reverse_shifted(char *input, int by)
|
|
{
|
|
if(!input)
|
|
{
|
|
return NULL;
|
|
}
|
|
int len = strlen(input);
|
|
char *res = calloc(len, sizeof(char));
|
|
if(!res)
|
|
{
|
|
return NULL;
|
|
}
|
|
int start = 0;
|
|
int end = len + 1;
|
|
for (int i = 0; i < len; i++)
|
|
{
|
|
char val = input[end--];
|
|
val = shift(val, by);
|
|
res[start++] = val;
|
|
}
|
|
return res;
|
|
}
|
|
|
|
int main(void)
|
|
{
|
|
char *test1 = "!qxsbdc k cs csrd";
|
|
char *res1 = reverse_shifted(test1, 42);
|
|
if(!res1)
|
|
{
|
|
return EXIT_FAILURE;
|
|
}
|
|
puts(res1);
|
|
free(res1);
|
|
return EXIT_SUCCESS;
|
|
}
|