Recent Post

Caesar Cipher.c-HackerRank

Problem-Julius Caesar protected his confidential information by encrypting it using a cipher. Caesar's Cipher shifts each letter by a number of letters. If the shift takes you past the end of the alphabet, just rotate back to the front of the alphabet. In the case of a rotation by 3, w, x, y and z would map to z, a, b and c.
Original alphabet: abcdefghijklmnopqrstuvwxyz
Alphabet rotated +3: defghijklmnopqrstuvwxyzabc

For example, the given cleartext  and the alphabet is rotated by . The encrypted string is .

Note: The cipher only encrypts letters; symbols, such as -, remain unencrypted.



Solution-
#include <assert.h>
#include <limits.h>
#include <math.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(){
    int n; 
    scanf("%d",&n);
    char* s = (char *)malloc(10240 * sizeof(char));
    scanf("%s",s);
    int k,x; 
    scanf("%d",&k);
    for(int i=0; i<=strlen(s);i++){
        if( isalpha(s[i]) ){
            if( isupper(s[i]) )
             x='A';
            else
               x='a';
           int a=(int)s[i];
               a= x+((a+k-x)%26);
           char c=a;
               s[i]=c;
        }        
    }
    printf("%s",s);
    return 0;
}

Link to the problem:-

Comments

Popular posts from this blog

Bon Appétit.c-HackerRank

Electronics Shop.c-HackerRank