This project reads a file of text and generates a cipher of the file. A cipher i
ID: 3596479 • Letter: T
Question
This project reads a file of text and generates a cipher of the file. A cipher is simply a secret or disguised way of writing the text. We disguise the text by shifting it to the right (or left) and also incrementing (or decrementing) the letters and digits in the text.
The user supplies, on the command line, an input file and a combination of letters (I or D or L or R) to create a unique cipher that is applied to the input file. Your program will read the file word by word. A word, also known as a token, is defined as a string bounded by white spaces but containing no white spaces. It applies the following algorithm to each word read from the input file.
I–incrementallthelettersanddigitsintheword. Thatis,anabecomesab,anXbecomesaY,a1becomes a 2, and so on. Ifyou have azor aZor a9, then these wrap around and become anaorAor0. Ifa character in the word is not a letter or a digit, it is kept unchanged.
D – decrement all letters and digits in the word. That is, a b becomes an a, an N becomes an M, a 5 becomes a4, and so on. Ifyou have anaorAor0, then these wrap around and become azorZor9. Again ifa character in the word is not a letter or a digit, it is kept unchanged.
L – shift all the characters in the word to the left one position. The character at position 0 (the start of the word) becomes the new last character in the word.
R – shift all the characters in the word right by one position. The last character in the word becomes the new first character (the start of the word).
Your program gets the name of the input file and the sequence of cipher commands from the command line. It writes the cipher to standard output (one token/word per line). As an example of how the program works, consider the input file shown below (named data)
Two quick comments on this program:
You must use at least four functions in your program and place their signatures at the beginning of the file.
One obvious approach is to have a function for each of the four cipher techniques.
Your program must print an error message and exit if the program fails to open the file specified for
reading, or if any of the “encryption” commands are not valid – must be one of I or D or L or R.
The University of Alabama *1831* Crimson Tide! ./a.out data L ./a.out data I ./a.out data LI./a.out data LIDR The University of Alabama 1831* Crimson Tide! heT niversityU labamaA 1831* rimsonC ide !T Voiwfstjuz Pg Bmbcbnb 2942* Dsjntpo Ujef! ifU ojwfstiuzV 9P mbcbnbB 2942** sjntpoD ef !UExplanation / Answer
#include <stdio.h>
#include <string.h>
int isValidEncryption(char *str); //check if the letters in the string is one of I , D , L or R
void increment(char *str); //increments letter or digit and wraps around if needed
void decrement(char *str); //decrements letter or digit and wraps around if needed
void shiftLeft(char *str); //shifts all characters by movin the 1st character to end
void shiftRight(char *str); //shifts all characters by movin the last character to beginning
int main(int argc, char *argv[])
{
FILE *fp;
char str[30];
char *commands;
int i;
if(argc != 3)
{
printf("Usage: %s <inputfile> <combination of I D L R> " , argv[0]);
return 1;
}
commands = argv[2];
if(!isValidEncryption(commands))
{
printf("Invalid combination of commands. Use only I D L or R " );
return 1;
}
fp = fopen(argv[1], "r");
if(fp == NULL)
{
printf("Could not open input file %s ", argv[0]);
return 1;
}
fscanf(fp, "%s", str);
while( !feof(fp))
{
for(i = 0; commands[i] != ''; i++)
{
switch(commands[i])
{
case 'I':
increment(str);
break;
case 'D':
decrement(str);
break;
case 'L':
shiftLeft(str);
break;
case 'R':
shiftRight(str);
break;
}
}
printf("%s ", str);
fscanf(fp, "%s", str);
}
fclose(fp);
}
int isValidEncryption(char *str)//check if the letters in the string is one of I , D , L or R
{
int i;
char ch;
for(i = 0; str[i] != ''; i++)
{
ch = str[i];
if(ch != 'I' && ch != 'D' && ch != 'L' && ch != 'R')
return 0;
}
return 1;
}
void increment(char *str)
{
char ch;
int i ;
for(i = 0; str[i] != ''; i++)
{
ch = str[i];
if(ch >= 'a' && ch <= 'z')
{
ch++;
if(ch > 'z') //need to wrap?
ch = 'a';
}
else if(ch >= 'A' && ch <= 'Z')
{
ch++;
if(ch > 'Z') //need to wrap?
ch = 'A';
}
else if (ch >= '0' && ch <= '9')
{
ch++;
if(ch > '9') //need to wrap?
ch = '0';
}
str[i] = ch;
}
}
void decrement(char *str)
{
char ch;
int i ;
for(i = 0; str[i] != ''; i++)
{
ch = str[i];
if(ch >= 'a' && ch <= 'z')
{
ch--;
if(ch < 'a') //need to wrap?
ch = 'z';
}
else if(ch >= 'A' && ch <= 'Z')
{
ch--;
if(ch < 'A') //need to wrap?
ch = 'Z';
}
else if (ch >= '0' && ch <= '9')
{
ch--;
if(ch < '0') //need to wrap?
ch = '9';
}
str[i] = ch;
}
}
void shiftLeft(char *str)
{
int i;
char ch1 = str[0]; //save the first char
for (i = 1; str[i] != ''; i++)
str[i-1] = str[i];
//put the saved char in end
str[i-1] = ch1;
}
void shiftRight(char *str)
{
int i;
int len = strlen(str);
char last = str[len-1]; //save the last char
for (i = len-1; i > 0 ; i--)
str[i] = str[i - 1];
//put the saved char in beginning
str[0] = last;
}
input file : data
The University of Alabama *1831* Crimson Tide!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.