Caesar Cipher - Easy Problems - Solution to Toph.co Online Judge Problem

Caesar Cipher

Limits 1s, 512 MB

Byang wants to write a secret message to his friend. He has recently discovered Caesar Cipher.
Caesar cipher is simple encryption techniques where each letter in the message is replaced by a letter some fixed number of positions down the alphabet.
For example, with a left shift of 2, C would be replaced by A, D would become B, and so on.
Letters:
ABCDEFGHIJKLMNOPQRSTUVWXYZ
After applying Caesar cipher of left shift 2:
YZABCDEFGHIJKLMNOPQRSTUVWX

Input

The first line of the input will contain one integer N (0 < N < 26), indicating the left shift of the cipher.
The second line will contain a message in all lower case alphabets and spaces. The message will contain at most 100 characters.

Output

Print the message after applying the Ceaser cipher using the specified left shift.

#include<stdio.h>
#include<string.h>

int main()
{
    char str[100],a,ch;
    int n,i,k=122;
  scanf("%d",&n);
  a=getchar();
gets(str);
 for(i = 0; str[i] != '\0'; ++i){
  ch = str[i];

  if(ch >= 'a' && ch <= 'z'){
   ch = ch - n;

   if(ch < 'a'){
    ch = ch + 26;
   }

   str[i] = ch;
  }
 }
    puts(str);
}