Jump to content

C Programming: caesar cipher for lower case letters only

Go to solution Solved by MeshFile,

Put the condition before the

input[i] = input[i] + rotate;

I have this code which uses the caesar cipher encryption method based on a users input, so the user enters the text then they enter the amount of times they want to shift the text.

 

However I was wondering how I could make it so only the lower case letters would be shifted as specified.

 

I was thinking of adding something like this:

if (input[i] >= 97 && input[i] <= 122)

But i'm not sure where to add it in my program which is this:

#include<stdio.h>  #include<string.h>  int main()  {        int rotate, i;        char input[80];        printf(" Enter the text you wish to be encrypted: \n");        gets(input);	        printf(" Enter the amount of times you want to rotate: \n");  	        scanf("%d",&rotate);  		        if (rotate > 26)      rotate = rotate % 26;  //0 <= rotate < 26.        for(i = 0; i < strlen(input); i++)        {            if(input[i] == ' ')           {            continue;        }       else        {             if(input[i] >= 'x')             {              input[i] = input[i] -26;              }             input[i] = input[i] + rotate;            }       }	       printf("Your encrypted text is: %s\n", input);  }

Could anyone advise me on this? I need only lower case letters to be caeser ciphered.


 

   
   
Link to post
Share on other sites

One thing I will point out is that you shouldn't use gets. It causes memory leaks and is a security risk. Use fgets instead.

[Out-of-date] Want to learn how to make your own custom Windows 10 image?

 

Desktop: AMD R9 3900X | ASUS ROG Strix X570-F | Radeon RX 5700 XT | EVGA GTX 1080 SC | 32GB Trident Z Neo 3600MHz | 1TB 970 EVO | 256GB 840 EVO | 960GB Corsair Force LE | EVGA G2 850W | Phanteks P400S

Laptop: Intel M-5Y10c | Intel HD Graphics | 8GB RAM | 250GB Micron SSD | Asus UX305FA

Server 01: Intel Xeon D 1541 | ASRock Rack D1541D4I-2L2T | 32GB Hynix ECC DDR4 | 4x8TB Western Digital HDDs | 32TB Raw 16TB Usable

Server 02: Intel i7 7700K | Gigabye Z170N Gaming5 | 16GB Trident Z 3200MHz

Link to post
Share on other sites

I did try that but it just gives me errors. gets seems to work

The formatting with fgets is bit different.

[Out-of-date] Want to learn how to make your own custom Windows 10 image?

 

Desktop: AMD R9 3900X | ASUS ROG Strix X570-F | Radeon RX 5700 XT | EVGA GTX 1080 SC | 32GB Trident Z Neo 3600MHz | 1TB 970 EVO | 256GB 840 EVO | 960GB Corsair Force LE | EVGA G2 850W | Phanteks P400S

Laptop: Intel M-5Y10c | Intel HD Graphics | 8GB RAM | 250GB Micron SSD | Asus UX305FA

Server 01: Intel Xeon D 1541 | ASRock Rack D1541D4I-2L2T | 32GB Hynix ECC DDR4 | 4x8TB Western Digital HDDs | 32TB Raw 16TB Usable

Server 02: Intel i7 7700K | Gigabye Z170N Gaming5 | 16GB Trident Z 3200MHz

Link to post
Share on other sites

fgets(name of array, size of array, stdin);

The stdin indicates that is scanning standard input. You have to  specify that because fgets can also be used to read from files.

 

Read more here: http://www.dummies.com/how-to/content/how-to-use-the-fgets-function-for-text-input-in-c-.html

[Out-of-date] Want to learn how to make your own custom Windows 10 image?

 

Desktop: AMD R9 3900X | ASUS ROG Strix X570-F | Radeon RX 5700 XT | EVGA GTX 1080 SC | 32GB Trident Z Neo 3600MHz | 1TB 970 EVO | 256GB 840 EVO | 960GB Corsair Force LE | EVGA G2 850W | Phanteks P400S

Laptop: Intel M-5Y10c | Intel HD Graphics | 8GB RAM | 250GB Micron SSD | Asus UX305FA

Server 01: Intel Xeon D 1541 | ASRock Rack D1541D4I-2L2T | 32GB Hynix ECC DDR4 | 4x8TB Western Digital HDDs | 32TB Raw 16TB Usable

Server 02: Intel i7 7700K | Gigabye Z170N Gaming5 | 16GB Trident Z 3200MHz

Link to post
Share on other sites

I did this some time ago .

void cypher(char *str, int offset){    for(int i = 0; i < strlen(str); i++)    {        if (islower(*(str+i)))        {            *(str+i) = ((26 + ((*(str+i) - int('a')) + (offset % 26))) % 26) + int('a');        }        else        {            /*Whatever*/        }    }}

strlen() is from string.h .

islower() is from ctype.h

 

And of course, you use it something like :

   char c[100];   fgets(c,100,stdin);   cypher(c, 13);   printf("%s",c);

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×