Untitled

mail@pastecode.io avatar
unknown
plain_text
6 months ago
1.3 kB
1
Indexable
Never
#include <stdio.h>
#include <string.h>
#include <ctype.h>

int main(){
    char before[] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
            'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r',
            's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };
    
    char after[] = { 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O',
            'P', 'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K',
            'L', 'Z', 'X', 'C', 'V', 'B', 'N', 'M' };

    char str[100], cipher[100] = "";
    printf("Input the normal text: ");
    fgets(str, 100, stdin);
    str[strcspn(str, "\n")] = 0;  // remove trailing newline
    printf("\nBefore encryption: %s\n", str);

    for(int j=0; j<strlen(str); j++){
        if(str[j] == ' '){
            strncat(cipher, " ", 1);
            continue;
        }
        for(int i=0; i<26; i++){
            if(before[i] == tolower(str[j])){
                char temp[2] = "";
                if(j==0 || str[j-1] == ' ') temp[0] = after[i];
                else if(j==strlen(str)-1 || str[j+1] == ' ') temp[0] = after[(i+6)%26];
                else temp[0] = after[(i+3)%26];
                strncat(cipher, temp, 1);
                break;
            }
        }
    }
    printf("After encryption: %s", cipher);
    return 0;
}
Leave a Comment