Search code examples
ccaesar-cipher

Why can't I print space while encrypting using Caesar cipher?


I am trying to write a C program that encrypts or decrypts a message using the Caesar cipher method. The user can enter the message with spaces, but my C program prints some other character (like an [], alpha symbol, or an alphabets sometimes). Can anyone suggest changes in my code to print the spaces?

#include <stdio.h>
#include <conio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>

void main()
{
    int  i,j,s,k,p,choice,key,n,count;
    char alpha[27]={'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 msg[100],encrypt[100];
    printf("\t\t\tCEASER CIPHER");
    printf("\n\t\t\t-------------");
    printf("\n1.Encrypt");
    printf("\n2.Decrypt");
    printf("\nEnter the choice:");
    scanf("%d",&choice);
    switch(choice){
    case 1 :{system("cls");
             printf("Enter the message to be encrypted:");
             fflush(stdin);
             gets(msg);

             printf("\nEnter the shift key:");
             scanf("%d",&key);
             n=strlen(msg);
             printf("\nThe encrypted key is :");
            for(i=0;i<n;i++){
                    count=0;
                for(j=0;j<27;j++){
                if(msg[i]==alpha[j]){
                s=j+key;
                k=s%26;
                encrypt[i]=alpha[k];
                count=1;
                break;
                }
             }
              if(count=1)
                printf("%c",encrypt[i]);
              else if(count=0)
              printf("-");
             }
             }
    case 2 :{
            }
             }
}
1.output:-
Enter the message to be encrypted:hello world
Enter the shift key:3
Th encrypted key is:khoor'zruog
expected output:-
Enter the message to be encrypted:hello world
Enter the shift key:3
Th encrypted key is:khoor-zruog
1.output:-
Enter the message to be encrypted:how are you
Enter the shift key:3
Th encrypted key is:krz-duhabrx
expected output:-
Enter the message to be encrypted:how are you
Enter the shift key:3
Th encrypted key is:krz-duh-brx

Solution

  • What you have done is basically correct, but not the right way to do so. You are using two loops to check if letter is in given above array or not. You can easily check that like this:

    if (msg[i] >= 'a' && msg[i] <= 'z')  // if msg[i] is letter
    

    So, it would be easier to write your code like this:

        for (int i = 0; i < n; ++i)
        {
            if (msg[i] >= 'a' && msg[i] <= 'z')
            {
                encrypt[i] = msg[i] + key;
    
                if (encrypt[i] > 'z')
                    encrypt[i] = 'a' + (encrypt[i] - 'z');  // or encrypt[i] -= 26;
            }
        }
    

    And also, as Johathan mentioned, you need break between cases. Moreover, as he said again, if (count = 1) is not checking the value of count: it just assigns it. If you need comparison, use == (although you do not need it anyway).