Question

In: Computer Science

Encrypting Text with a Caesar Cipher Write a C program caesar.c which reads characters from its...

Encrypting Text with a Caesar Cipher

Write a C program caesar.c which reads characters from its input and writes the characters to its output encrypted with a Caesar cipher.

A Caesar cipher shifts each letter a certain number of positions in the alphabet.

The number of positions to shift will be given to your program as a command line argument.

Characters other than letters should not be encrypted.

Your program should stop only at the end of input.

Your program should contain at least one function other than main.

For example:

./caesar 1
This life well it's slipping right through my hands
Uijt mjgf xfmm ju't tmjqqjoh sjhiu uispvhi nz iboet
These days turned out nothing like I had planned
Uiftf ebzt uvsofe pvu opuijoh mjlf J ibe qmboofe

./caesar 10
abcdefghijklmnopqrstuvwxyz
klmnopqrstuvwxyzabcdefghij
ABCDEFGHIJKLMNOPQRSTUVWXYZ
KLMNOPQRSTUVWXYZABCDEFGHIJ

./caesar -42
Control well it's slipping right through my hands
Myxdbyv govv sd'c cvszzsxq bsqrd drbyeqr wi rkxnc
These days?
Droco nkic?

Hint: handle upper and lower case letters separately

Hint: use %

Hint: use atoi to convert the first command-line argument to an int.

Hint:make sure you understand this example program which uses a atoi to convert command-line arguments to an ints.

Hint: create a function with a prototype like this:

int encrypt(int character, int shift);

which returns the character shifted by the specified amount

Manually Cracking a Caesar Cipher

Here is some (New Zealand) English text that has been encrypted with a Caesar cipher.

Z uf dp drbvlg ze jfdvsfup vcjv'j tri
Nv fiuvi uzwwvivek uizebj rk kyv jrdv srij
Z befn rsflk nyrk pfl uzu reu Z nreer jtivrd kyv kilky
Jyv kyzebj pfl cfmv kyv svrty, pfl'iv jlty r urde czri

Use the program you have just written to discover the secret text?

Hint:: try different shifts until you see English.

Solutions

Expert Solution

ANSWER :-

GIVEN THAT :-

#include <stdio.h>
#include <string.h>
//Caesar method
void encrypt(char str[], int key)
{
int i;
char c;
key=key%26;
if (key <0)
key=key+26;
// defining new character array
char s[200];
// Iterating in String
for(i = 0; str[i] != '\0'; ++i){
       c = str[i];
       // Lowercase check
       if(c >= 'a' && c <= 'z'){
           c = (char)((int)(str[i]+key-97)%26 +97);
       }
       //Upper case check
       else if(c >= 'A' && c <= 'Z'){
           c = (char)((int)(str[i]+key-65)%26 +65);
          
       }
       s[i]=c;
   }
   // Printing
   printf("%s", s);
}

// Driver function
int main()
{
int n;
// Character array
char s[200];
scanf("%d \n",&n);
// string Input
fgets(s, sizeof(s), stdin);
// method call
encrypt(s,n);

return 0;
}

SCREEN SHOT :-

OUTPUT :-


Related Solutions

Using CrypTool2, display the results of encrypting the following plain text using the Caesar Cipher method...
Using CrypTool2, display the results of encrypting the following plain text using the Caesar Cipher method and a left shift value of 3. MEET YOU AT THE FOOTBALL MATCH
In C++, write a program to implement the Caesar Cipher for both encryption and decryption. The...
In C++, write a program to implement the Caesar Cipher for both encryption and decryption. The program should be able to handle different keys by deciding the key at run time. Thank you :)
Using c++, write a program that reads a sequence of characters from the keyboard (one at...
Using c++, write a program that reads a sequence of characters from the keyboard (one at a time) and creates a string including the distinct characters entered and displays the string on the screen. The input terminates once the user enters a white-space character or the user has entered 50 distinct characters. Do not use C-Strings. 2. Use the following function to append character “ch” to the string “s”: s.push_back(ch); 3. Read the input characters one by one, i.e. do...
In C++, write a program that reads data from a text file. Include in this program...
In C++, write a program that reads data from a text file. Include in this program functions that calculate the mean and the standard deviation. Make sure that the only global variables are the actual data points, the mean, the standard deviation, and the number of data entered. All other variables must be local to the function. At the top of the program make sure you use functional prototypes instead of writing each function before the main function... ALL LINES...
Write a C++ program that reads a string from a text file and determines if the...
Write a C++ program that reads a string from a text file and determines if the string is a palindrome or not using stacks and queue
Write a Java program that will encode plain text into cipher text and decode cipher text...
Write a Java program that will encode plain text into cipher text and decode cipher text into plain text. Create following methods and add them to your class: • a method named encode(String p, int key) that takes a plain text p and encodes it to a cipher text by adding the key value to each alphabet character of plain text p using the ASCII chart. The method should return the encoded String. For example, if p = "attack at...
Write a program FileCounter.java that reads a text file text1.txt and reports the number of characters...
Write a program FileCounter.java that reads a text file text1.txt and reports the number of characters and lines contained in the file. For example, the output should be as follows: Lines: 5 Chars: 124 Note: Write a main() method to test in the FileCounter.java. The file path should use relative path. Must use the provided text file text1.txt. The text file say This is an example 1 This is an example 1 2. This is an example 1 2 3....
Write the program in java Write a program that does basic encrypting of text. You will...
Write the program in java Write a program that does basic encrypting of text. You will ask the user the filename of a text file which contains a few sentences of text. You will read this text file one character at a time, and change each letter to another one and write out to an output file. The conversion should be done a -> b, b->c , … z->a, A->B, B->C, …. Z->A. This means just shift each letter by...
Write Java program Lab52.java which reads in a line of text from the user. The text...
Write Java program Lab52.java which reads in a line of text from the user. The text should be passed into the method: public static String[] divideText(String input) The "divideText" method returns an array of 2 Strings, one with the even number characters in the original string and one with the odd number characters from the original string. The program should then print out the returned strings.
Using C++ Write a program that reads a text from the keyboard until the user presses...
Using C++ Write a program that reads a text from the keyboard until the user presses the “Enter” key. Your program must count the number of uppercase alphabets (A through Z), lowercase alphabets (a through z), digits (0 through 9) and any other characters. The other character count value should NOT include the “Enter” key. Display the count on the screen. You must use the in-built C++ character I/O functions in this program.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT