Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <stdbool.h>
- #include <assert.h>
- bool isAlphabetValid(char* alphabet) {
- assert(alphabet);
- if (!*alphabet)
- return false;
- for (; *alphabet; alphabet++)
- if (strchr(alphabet + 1, *alphabet))
- return false;
- return true;
- }
- bool isStringValid(char* string, char* alphabet, char* exclude) {
- assert(string);
- assert(alphabet);
- if (!*string)
- return false;
- if (exclude)
- for (; *string; string++) {
- if (strchr(exclude, *string))
- continue;
- if (!strchr(alphabet, *string))
- return false;
- }
- else
- for (; *string; string++)
- if (!strchr(alphabet, *string))
- return false;
- return true;
- }
- inline ptrdiff_t getIndex(char* string, int c) {
- return strchr(string, c) - string;
- }
- void Caesar_Cipher(char* string, char* alphabet, char* exclude, unsigned shift) {
- if (!isAlphabetValid(alphabet) || !isStringValid(string, alphabet, exclude))
- return;
- size_t alphabet_size = strlen(alphabet);
- if (exclude)
- for (; *string; string++) {
- if (strchr(exclude, *string))
- continue;
- *string = alphabet[(getIndex(alphabet, *string) + shift) % alphabet_size];
- }
- else
- for (; *string; string++)
- *string = alphabet[(getIndex(alphabet, *string) + shift) % alphabet_size];
- }
- char alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
- char exclude[] = " ,.!?";
- char MyString[] = "HELLO, MY NAME IS ALEX! WHATS YOU?";
- int main()
- {
- printf("\"%s\"\n", MyString);
- Caesar_Cipher(MyString, alphabet, NULL, 3);
- printf("\"%s\"\n", MyString);
- Caesar_Cipher(MyString, alphabet, exclude, 1);
- printf("\"%s\"\n", MyString);
- Caesar_Cipher(MyString, alphabet, exclude, 0);
- printf("\"%s\"\n", MyString);
- Caesar_Cipher(MyString, alphabet, exclude, 25);
- printf("\"%s\"\n", MyString);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement