Question

Create a C program: A caesar cipher is a simple cipher that shifts letters in a...

Create a C program:

A caesar cipher is a simple cipher that shifts letters in a string. For example, shifting “ab” over by 1 would result in “bc”, and shifting “xyz” over by 2 would result in “zab”. The caesar program should take, in the command line an integer k, the amount to shift some text by, and a string f i l e, the name of a file containing text to encode using the cipher.

For example, suppose “secret.txt” contains “defend the east wall of the castle”, then running caesar with 3 should result in the following:
$ cat secret.txt
defend the east wall of the castle
$ ./caesar 3 secret.txt
ghihqg wkh hdvw zdoo ri wkh fdvwoh
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Hi... Please chekc the below c code.

Main.c

#include<stdio.h>
#include<string.h>
int main(int argc,char* argv[])
{
int counter;
int val = 0;
//printf("Program Name Is: %s",argv[0]);
if(argc==1)
printf("\nNo Extra Command Line Argument Passed Other Than Program Name");
if(argc>=2)
{
val = atoi(argv[1]);
//printf("\n%s: %s",argv[1],argv[2]);

char c[1000];
FILE *fptr;

if ((fptr = fopen(argv[2], "r")) == NULL)
{
printf("Error! opening file");
// Program exits if file pointer returns NULL.
exit(1);
}

// reads text until newline
fscanf(fptr,"%[^\n]", c);

printf("Data from the file:\n%s", c);
fclose(fptr);
  
char new[strlen(c)];
int index = 0;
char smallalphabet[26] = {'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'};
for(int i=0;i<strlen(c);i++){
bool flag = false;
for(int k=0;k<26;k++){
  
if(c[i]==smallalphabet[k]){
index = k+val;
if(index>25){
index = index - 26;
}
new[i] = smallalphabet[index];
//printf("matched\n");
flag = true;
break;
}
  
}
if(!flag){
new[i] = c[i];
}
  
}
  
printf("\nOutput file:\n%s", new);
}
return 0;
}

Input:

example.txt

abcd

Output: --- /Main.c 3 example.txt

Data from the file:                                                                                                   

abcd                                                                                                                  

Output file:                                                                                                   

defg

Please text the code and let me know querries. Thank you. All the best.

Add a comment
Know the answer?
Add Answer to:
Create a C program: A caesar cipher is a simple cipher that shifts letters in a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Caesar Cipher v3 Decription Description A Caesar cipher is one of the first and most simple...

    Caesar Cipher v3 Decription Description A Caesar cipher is one of the first and most simple encryption methods. It works by shifting all letters in the original message (plaintext) by a certain fixed amount (the amounts represents the encryption key). The resulting encoded text is called ciphertext. Example Key (Shift): 3 Plaintext: Abc Ciphertext: Def Task Your goal is to implement a Caesar cipher program that receives the key and an encrypted paragraph (with uppercase and lowercase letters, punctuations, and...

  • Assignment 7: Caesar Cipher Assignment 7 You will create a Caesar cipher which allows the user...

    Assignment 7: Caesar Cipher Assignment 7 You will create a Caesar cipher which allows the user to specify the key and the text to be encrypted. A Caesar cipher is a simple substitution cipher wherein each letter in the message is shifted a certain number of spaces down the alphabet -- this number is called the key. a b c d e f g h i j k l m n o p q r s t u v w...

  • Using the website Repl.it Write a Java program that can perform the Caesar cipher for English...

    Using the website Repl.it Write a Java program that can perform the Caesar cipher for English messages that include both upper and lowercase alphabetic characters. The Caesar cipher replaces each plaintext letter with a different one, by a fixed number of places down the alphabet. The program should be able to shift a specific number of places based on the user's input number. For example Plaintext: THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG. Ciphertext: QEBNRFZH YOLTK CLU GRJMP...

  • USING C LANGUAGE Write a program to compute a Caesar Cipher . You must read in...

    USING C LANGUAGE Write a program to compute a Caesar Cipher . You must read in a file of text and convert some of the letters to another letter using the following array: char key[27] = “efghijklmnopqrstuvwxyzabcd”; This means that if you encounter the letter ‘a’, then you will replace it with the letter ‘e’,etc. If you encounter any other characters in the input file (example: ‘A’ or space, etc, you will write them as is. You will write each...

  • using the website repl.it (must be done in Javascript) PGM #1 Write a Java program that can perform the Caesar cipher fo...

    using the website repl.it (must be done in Javascript) PGM #1 Write a Java program that can perform the Caesar cipher for English messages that include both upper and lowercase alphabetic characters. The Caesar cipher replaces each plaintext letter with a different one, by a fixed number of places down the alphabet. The cipher illustrated here uses a left shift of three, so that (for example) each occurrence of E in the plaintext becomes B in the ciphertext. For example...

  • Using C++ Part C: Implement the modified Caesar cipher Objective: The goal of part C is...

    Using C++ Part C: Implement the modified Caesar cipher Objective: The goal of part C is to create a program to encode files and strings using the caesar cipher encoding method. Information about the caesar method can be found at http://www.braingle.com/brainteasers/codes/caesar.php.   Note: the standard caesar cipher uses an offset of 3. We are going to use a user supplied string to calculate an offset. See below for details on how to calculate this offset from this string. First open caesar.cpp...

  • create a Java class ShiftCipher The program ShiftCipher should take two inputs from the terminal. The...

    create a Java class ShiftCipher The program ShiftCipher should take two inputs from the terminal. The first should be a string of any length which contains any type of symbol (the plaintext). The second will be a shift value which should be between 0 and 25 inclusive (though you may design your program to be resilient to shifts beyond this value). The program should print the cipher text, in which each of the alphabetic characters in the string is shifted...

  • ior new tr k%2005%281 %29, You are just hired by Google as a senior software developer...

    ior new tr k%2005%281 %29, You are just hired by Google as a senior software developer and your supervisor assigns you the following task: You are responsible for the development of a program that encrypts and decrypts text messages using the Caesar shift, which is one of the oldest and widely known forms of encryption Caesar shift is a type of substitution encryption method in which each letter in the text is 'shifted' a fixed mumber of positions down the...

  • Help write down below program with C++ language!!! Please... The Cipher Program Requirements An interactive program...

    Help write down below program with C++ language!!! Please... The Cipher Program Requirements An interactive program is required that allows a user to encode text using any of three possible ciphers. The three ciphers you are to offer are: Caesar, Playfair and Columnar Transposition. • The program needs to loop, repeating to ask the user if they wish to play with Caesar, Playfair or Columnar Transposition until the user wishes to stop the program. •For encoding, the program needs to...

  • C Program In this assignment you'll write a program that encrypts the alphabetic letters in a...

    C Program In this assignment you'll write a program that encrypts the alphabetic letters in a file using the Vigenère cipher. Your program will take two command line parameters containing the names of the file storing the encryption key and the file to be encrypted. The program must generate output to the console (terminal) screen as specified below. Command Line Parameters Your program must compile and run from the command line. The program executable must be named “vigenere” (all lower...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT