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

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.
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++ coding question From the text file given to you- “worldpop.txt”, perform the following tasks using...
C++ coding question From the text file given to you- “worldpop.txt”, perform the following tasks using Boolean function. PS-*Write separate codes for each task* Task 1. Display the names of the countries with: 1. Population >=1000,000,000 2. Population <= 1000,000 Task 2. Display the names of the first 10 countries Task 3. Display the names of the last 10 countries contents of worldpop.txt: Afghanistan 32738376 Akrotiri 15700 Albania 3619778 Algeria 33769669 Andorra 72413 Angola 12531357 Anguilla 14108 Argentina 40677348 Armenia...
Assignment: Square Matrix ("Caesar Block")Cipher C++ ASSIGNMENTS *REMEMBER IT SHOULD NOT ONLY READ ONE LINE OR...
Assignment: Square Matrix ("Caesar Block")Cipher C++ ASSIGNMENTS *REMEMBER IT SHOULD NOT ONLY READ ONE LINE OR A SINGLE WORD remember, read, understand, and use the waterpump.cpp program will process an arbitrary amount of input text, however long. Arrange a stream in a two dimensional array of characters by filling up the array a line at a time, from leftmost column to rightmost column. DO NOT USE A GETLINE: this program should be able to input an entire library, not a...
Systems Programming - File Operations in Linux using C Preventing copying the same file What does...
Systems Programming - File Operations in Linux using C Preventing copying the same file What does the standard cp do if you try to copy a file onto itself? $ cp file1 file1 cp: 'file1' and 'file1' are the same file Modify cp1.c to handle the situation and include comments. /** * @file cp1.c * @brief Uses read and write with tunable buffer size * usage: cp1 src dest */ #include <stdio.h> #include <unistd.h> #include <fcntl.h> #include <stdlib.h> #define BUFFERSIZE...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT