Question

In: Computer Science

Programming Language C++ Encrypt a text file using Caesar Cipher. Perform the following operations: Read the...

Programming Language C++

Encrypt a text file using Caesar Cipher.

Perform the following operations:

  • Read the console input and create a file. ['$' character denotes end of content in file.]
  • Close the file after creation.
  • Now encrypt the text file using Caesar Cipher (Use key value as 5).
  • Display the contents of the updated file.

#include <iostream>

using namespace std;

int main() {

// write code here

}

Solutions

Expert Solution

#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
void encrypt(char *text, int s)
{
   FILE *fptr;
   fptr = fopen("program.txt", "w+");
   int len = strlen(text);
char result[len+1];
int j=0 ;
for (int i=0;i<strlen(text);i++)
{
if (isupper(text[i]))
result[j]= char(int(text[i]+s-65)%26 +65);
  
       else if(islower(text[i]))
           result[j]= char(int(text[i]+s-97)%26 +97);
else
           result[j]=text[i];
j++;
}
   result[j]='\0';
fprintf(fptr,"%s",result);
fclose(fptr);
}
int main()
{
   char buffer[1000];
   char buff[1000];
FILE *fptr;
fptr = fopen("program.txt", "w+");
if (fptr == NULL) {
printf("Error!");
exit(1);
}
printf("Enter the text to be tested for cipher use $ as last character and press enter to end\n");
fgets(buffer, sizeof(buffer), stdin);
fprintf(fptr,"%s", buffer);
fclose(fptr);
fptr = fopen("program.txt", "r");
char c;
if (fptr == NULL)
{
printf("Cannot open file \n");
exit(0);
}
c = fgetc(fptr);
int i=0;
while (c!='$'&&c != EOF)
{
buffer[i]=c;
c = fgetc(fptr);
i++;
}
buffer[i]='\0';
fclose(fptr);
encrypt(buffer, 5);
fptr = fopen("program.txt", "r");
if (fptr == NULL)
{
printf("Cannot open file \n");
exit(0);
}
cout<<"File content is"<<endl;
c = fgetc(fptr);
while (c != EOF)
{
printf ("%c", c);
c = fgetc(fptr);
}

   return 0;
}


Related Solutions

Hi, (C programming) I am looking to write a program that will encrypt a text file...
Hi, (C programming) I am looking to write a program that will encrypt a text file automatically once program has opened, and have the option to decrypt it in a menu in my program. I have multiple text files that I need to encrypt, and would like if the program could encrypt all of them at once. I would also only like to decrypt the a text file once the name has been entered into a scanf function.
Using c programming language How do you put data from a text file into a 2d...
Using c programming language How do you put data from a text file into a 2d array For example a text file with names and age: john 65 sam 34 joe 35 sarah 19 jason 18 max 14 kevin 50 pam 17 bailey 38 one 2d array should have all the names from the file and one 2d array should have all the ages and both arrays should be printed out separately and be 3x3
For c language. I want to read a text file called input.txt for example, the file...
For c language. I want to read a text file called input.txt for example, the file has the form. 4 hello goodbye hihi goodnight where the first number indicates the n number of words while other words are separated by newlines. I want to store these words into a 2D array so I can further work on these. and there are fewer words in the word file than specified by the number in the first line of the file, then...
In C Programming Language Write a program to output to a text log file a new...
In C Programming Language Write a program to output to a text log file a new line starting with day time date followed by the message "SUCCESSFUL". Please screenshot the results.
C Programming How do you read in a text file line by line into a an...
C Programming How do you read in a text file line by line into a an array. example, i have the text file containing: line 1: "qwertyuiop" line 2: "asdfghjkl" line 3: "zxcvbnm" Then in the resulting array i get this: array:"qwertyuiopasdfghjklzxcvbnm"
Binary file IO Suppose a file has been encrypted using the Caesar cipher as described above,...
Binary file IO Suppose a file has been encrypted using the Caesar cipher as described above, and you know the secret key. Write a program to decrypt (or decode) the file. Your program will prompt the user to enter an input file name for the encrypted file, an output file name for the unencrypted version of the input file, and the secret key. Create a DataInputStream for the input file and a DataOutputStreams for the output file. Next, read the...
Write a C Program that uses file handling operations of C language. The Program should perform...
Write a C Program that uses file handling operations of C language. The Program should perform following operations: 1. The program should accept student names and students’ assignment marks from the user. 2. Values accepted from the user should get saved in a .csv file (.csv files are “comma separated value” files, that can be opened with spreadsheet applications like MS-Excel and also with a normal text editor like Notepad). You should be able to open and view this file...
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...
Assignment in C programming class: read the text file line by line, and place each word,...
Assignment in C programming class: read the text file line by line, and place each word, in order, in an array of char strings. As you copy each word into the array, you will keep a running count of the number of words in the text file and convert the letters of each word to lower case. Assume tgat you will use no more than 1000 words in the text, and no more than the first 10 characters in a...
C++ programming question Write a program that will read input from a text file called "theNumbers.txt"...
C++ programming question Write a program that will read input from a text file called "theNumbers.txt" (you will have to provide your own when debugging). The text file that is to be opened is formatted a certain way, namely, there is always one integer, one character (representing an operation), another integer, and then a new line character, with spaces between each item. A sample text file is provided below. theNumbers.txt 144 + 26 3 * 18 88 / 4 22...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT