Contact Learn C
Copy

Program 209:Caesar Code Cipher in C

Program 209:
 
#include<stdio.h>
#include<string.h>
main()
{
//A-X,B-Y,C-Z,D-A,E-B,F-C... if number=1
//A-W,B-X,C-Y,D-Z,E-A,F-B... if number=2 and so on
 
 int i,number;
 char str[1000];
 printf("Enter a sentence\n");
 gets(str); 
 printf("Enter the number to change alphabetical order\n");
 scanf("%d",&number);
 i=0;
 number%=26;
 printf("Encrypted Caesar code\n\n");
  while(str[i]!='\0')
  {
     if(!((str[i]>=0&&str[i]<65)||(str[i]>90&&str[i]<97)||(str[i]>122&&str[i]<=127)))
     {
       if((str[i]>'a'+number+1)&&(str[i]<='z'))
       printf("%c",str[i]-number-2);
       else if((str[i]>'A'+number+1)&&(str[i]<='Z'))  
       printf("%c",str[i]-number-2);
       else
       printf("%c",str[i]+24-number);
     } 
    
    if(((str[i]>=0&&str[i]<65)||(str[i]>90&&str[i]<97)||(str[i]>122&&str[i]<=127)))
    {
      printf("%c",str[i]);    
    }
    
   i++;
  }
  printf("\n");
 
}

Explanation:

Before Going into Explanation remember that
A-X,B-Y,C-Z,D-A,E-B,F-C... if number=1
A-W,B-X,C-Y,D-Z,E-A,F-B... if number=2 and so on
We are interpreting A as X,and B as Y and so on when we given number as 1.The same will be different when number is 2 i.e A will be W which is the letter that comes before X so it will be like rotation when ever you change the number.If number is 3 then A=V and so on

Then What if number is 27 which went out of 26 but Alphabets are 26 only which is not possible to map alphabets.At this point we will do 27%6 which will give us remainder as output.So output of 27%6 is 1.So what ever the number (such as 2900 ,1080 etc) you give output will be in between 1 and 26 only so that it is easier to map Alphabets.

Since A is mapped to X and D is mapped to A when number is 1.Whenever user enter D we have to decrease it by 3 times to get A i.e D-3→ 68-3=65 which is A so for this we are doing D-number-2.
For Example if number=2
E is mapped to A. So to get A we are doing E-number-2→ 69-2-2=65 which is A.
This is how we are mapping.
Lets take A-X as left hand side alphabet as A and right hand side Alphabet as X
But before printing or mapping like above we have to check whether the lefthand side Alphabet

Now how to map A to X or A to W etc??
str[i]+24-number.lets take number=1
For Example.say str[i]=AA+24-number→65+24-188 which is X

So this is the main thing you need to read before going into explanation of program.

Now lets go into Program Explanation:


  1. The program starts with initializing
    • number → To store number to decide mapping of Alphabets
    • i Temporary variable
    • str[1000]→ To store sentence entered by user which has to be encrypted
  2. printf("Enter a sentence\n");
    gets(str); 
    printf("Enter the number to change alphabetical order\n");
    scanf("%d",&number);
    
    To take sentence and number from user.Lets take str="Abd1"
  3. i=0;
    number%=26;
    i=0 to start traversing from 1st alphabet to last one from the enetered sentence(say Hello World to traverse from H to d).And number%=26 as discussed above before starting program explanation.
  4. while(str[i]!='\0')
      {
         if(!((str[i]>=0&&str[i]<65)||(str[i]>90&&str[i]<97)||(str[i]>122&&str[i]<=127)))
         {
           if((str[i]>'a'+number+1)&&(str[i]<='z'))
           printf("%c",str[i]-number-2);
           else if((str[i]>'A'+number+1)&&(str[i]<='Z'))  
           printf("%c",str[i]-number-2);
           else
           printf("%c",str[i]+24-number);
         } 
        
        if(((str[i]>=0&&str[i]<65)||(str[i]>90&&str[i]<97)||(str[i]>122&&str[i]<=127)))
        {
          printf("%c",str[i]);    
        }
        
       i++;
      }
    Let us assume str="Abd1" nad number=1. The loop will traverse from str[i]=A to 1.Since every String is appended by '\0' by default at the end which is not shown outside to users
    • Iteration 1:i=0,str[i]→ str[0]='A'
      • if(!((str[i]>=0&&str[i]<65)||(str[i]>90&&str[i]<97)||(str[i]>122&&str[i]<=127)))
             {
        Checking if the Alphabet i.e str[0] belongs to numbers or special characters etc other than alphabet.And if it is only Alphabet we are going to encrypt the alphabet other wise not.For this refer ASCII Chart as alphabets will lie in between 65-90(A-Z) and 97-122(a-z)
      • Since 'A' is alphabet if part will execute
      • if((str[i]>'a'+number+1)&&(str[i]<='z'))
         printf("%c",str[i]-number-2);
        else if((str[i]>'A'+number+1)&&(str[i]<='Z'))  
          printf("%c",str[i]-number-2);
        else
          printf("%c",str[i]+24-number);
        str[i]>'a'+number+1 means what??
      • since we took number=1. 'a'+1+1='c' in characters or 99 in integers wise.
      • str[0]='A' we are checking 'A'>'a'+1+1→ 'A'>'c' i.e 65>99(in integer wise). Since its not true 1st if part won't execute
      • else if((str[i]>'A'+number+1)&&(str[i]<='Z')) 
        Now 'A'>'A'+1+1→'A'>65+1+1→65>67 which is false and 'A'<'Z' ehich is true but due to && operator both conditions should be true but one of the condistions is false here so 2nd if part also wont execute
      • So finally else part will execute and it will print str[i]+24-number→'A'+24-number→65+24-1→88 which is 'X' from ASCII sheet.
      • X will be printed first for the given character 'A'
    • Iteration 2:i=1,str[i]→ str[1]='b'
      • if(!((str[i]>=0&&str[i]<65)||(str[i]>90&&str[i]<97)||(str[i]>122&&str[i]<=127)))
             {
        Checking if the Alphabet i.e str[1] belongs to alphabet or not
      • Since 'b' is alphabet if part will execute
      • if((str[i]>'a'+number+1)&&(str[i]<='z'))
         printf("%c",str[i]-number-2);
        else if((str[i]>'A'+number+1)&&(str[i]<='Z'))  
          printf("%c",str[i]-number-2);
        else
          printf("%c",str[i]+24-number);
      • since we took number=1. 'a'+1+1='c' in characters or  99 in integers wise.
      • str[1]='b' we are checking 'b'>'a'+1+1→ 'b'>'c' i.e 98>99(in integer wise). Since its not true 1st if part won't execute
      • else if((str[i]>'A'+number+1)&&(str[i]<='Z')) 
        Now 'b'>'A'+1+1→'b'>65+1+1→98>67 is true but 'b'<'Z' i.e 98<90 which is false so 2nd if part also wont execute
      • So finally else part will execute and it will print str[i]+24-number→'b'+24-number→98+24-1→121 which is 'y'.
      • will be printed first for the given character 'b'
      • Till now Xy is printed
    • Iteration 3:i=2,str[i]→ str[2]='b'
      • if(!((str[i]>=0&&str[i]<65)||(str[i]>90&&str[i]<97)||(str[i]>122&&str[i]<=127)))
             {
        Checking if the Alphabet i.e str[2] belongs to alphabet or not
      • Since 'd' is alphabet if part will execute
      • if((str[i]>'a'+number+1)&&(str[i]<='z'))
         printf("%c",str[i]-number-2);
        else if((str[i]>'A'+number+1)&&(str[i]<='Z'))  
          printf("%c",str[i]-number-2);
        else
          printf("%c",str[i]+24-number);
      • since we took number=1. 'a'+1+1='c' in characters or  99 in integers wise.
      • str[2]='d' we are checking 'd'>'a'+1+1→ 'd'>'c' i.e 100>99(in integer wise). Since it is true and 'd'<='z' which is also true so 1st if part will execute and 2nd if part and else part won't execute
      • str[2]-number-2 will be printed  d-1-2=100-1-2=97 which is 'a'
      • will be printed for the given character 'd'
      • Till now Xya is printed
    • Iteration 4:i=3,str[3]='1'
      •  if(!((str[i]>=0&&str[i]<65)||(str[i]>90&&str[i]<97)||(str[i]>122&&str[i]<=127)))
         {
        Since '1' is a number the above if part won't execute the 2nd if part in loop will be executed
      •  if(((str[i]>=0&&str[i]<65)||(str[i]>90&&str[i]<97)||(str[i]>122&&str[i]<=127)))
          {
              printf("%c",str[i]);    
           }
        This if part has been used to print the character which is other than alphabet so '1' is printed as it is
      • Finally Output console will have 'Xya1' as encrypted text
  5. Finally Encrypted text for 'Abd1'-----> 'Xya1'
Output:
After 1st run:
 After 2nd run:
 
 After 3rd run:









 
Donate

Download App and Learn when ever you want

Get it on PlayStore
Get it on Amazon App Store
Get it on Aptoide