-1

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
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
alvin lal
  • 138
  • 4
  • 10
  • Note that your messaging suggests you're implementing the 'CEASER CIPHER', which is a cipher I've not heard of. Accurate spelling is important in programming. – Jonathan Leffler Sep 01 '19 at 08:02
  • See [Why the `gets()` method is too dangerous to be used — ever!](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-dangerous-why-should-it-not-be-used) – Jonathan Leffler Sep 01 '19 at 08:03
  • The code `if(count=1) printf("%c",encrypt[i]); else if(count=0) printf("-");` does not do what you think it should — you don't want assignments in these conditions. You need to turn up the warning level on your compiler so it complains about such trivial bugs. If you can't turn up the warnings that much, you need to get a better compiler. – Jonathan Leffler Sep 01 '19 at 08:04
  • You need a `break;` before `case 2:` — as written, the code flows into the (currently empty) decode section. – Jonathan Leffler Sep 01 '19 at 08:07
  • You should either copy the original unchanged character to the encrypted string, or initialize the string to blanks and copy encrypted letters, losing all non-encrypted (unencryptable) characters. – Jonathan Leffler Sep 01 '19 at 08:10

1 Answers1

0

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).

Miradil Zeynalli
  • 423
  • 4
  • 18
  • it prints the first three letters of the message that the user inputs, `#include void main() { int key,i,n; char encrypt[50],msg[50]; printf("\nEnter the message to be encrypted:"); scanf("%[^\n]s",&msg[50]); printf("\nEnter the shift key: "); scanf("%d",&key); printf("\nThe encrypted message is :"); n=strlen(msg); for(i=0;i= 'a' && msg[i]<= 'z'){ encrypt[i]=msg[i]+key; if(encrypt[i]>'z') encrypt[i]='a'+(encrypt[i]-'z'); } printf("%c",encrypt[i]); } }` – alvin lal Sep 09 '19 at 07:02
  • What did you give as an input? And maybe you should use, `gets` instead of `scanf` (print `n` before loop, so that, be sure you read whole string) – Miradil Zeynalli Sep 09 '19 at 07:12
  • I examined your code, and you have a **HUGE** mistake. `scanf("[^\n]s", &msg[50])` is not the way you write `scanf`. It should be `scanf("[^\n]s", msg)` – Miradil Zeynalli Sep 09 '19 at 10:52
  • it works,but if i am trying to encrypt xyz with shift key 7 it prints ffc,that is whenever the program goes to encrypt[i]>'z',the output is wrong – alvin lal Sep 10 '19 at 06:55
  • Oh right. There should be extra -1 in that formula, when it goes over 'z’ – Miradil Zeynalli Sep 10 '19 at 06:58
  • so should it be 'a'+((encrypyt[i]-'z')-1) ? – alvin lal Sep 10 '19 at 07:22
  • Yes, i think so – Miradil Zeynalli Sep 10 '19 at 07:24
  • still doesnt work,it print eCu for xyz,even though the logic is correct – alvin lal Sep 10 '19 at 07:35
  • with 'xyz' as input and key as 3, i got output 'abc' which is correct. Did you solve it somehow? – Miradil Zeynalli Sep 10 '19 at 16:07
  • It is the same, as yours (that u send here, in the comments) with `scanf(“%[^\n]s”, msg)` instead of `scanf(“%[^\n]s”, &msg[50])`. I just copied your code, and edited that – Miradil Zeynalli Sep 11 '19 at 17:23