Question

In: Computer Science

Write a C program that will read a character string and then encrypt the string based...

Write a C program that will read a character string and then encrypt the string based on one of the 3 different encryption methods. The type of encryption is to be selected by the user. Encryption method 1: Swapping by position. Characters in the array are swapped with the opposite characters based on their position in the string. Example: Input string – apple. Encrypted string – elppa Method: The first character ‘a’ and the last character ‘e’ – swap their position. – eppla The second character ‘p’ and the one before the last character ‘l’ swap their position – elppa Encryption method 2: Change position. Characters are moved in the forward by specific number of positions. The shifting of the characters to be based on a circle, with the characters in the end being inserted at the beginning of the new string. As the first character moves one step, the last character of the string is to be inserted as the first character of the encrypted string. User inputs the number of positions to be moved forward. Note: The number of positions by which the first character to be moved cannot exceed the length of the string. Example: Input string – apple Number of positions to be shifted – 3 Encrypted string - pleap Method: The first character is moved by 3 positions – Moved by 1 position Moved by 2 positions Moved by 3 positions eappl leapp pleap Encryption Method 3: Opposite Switch. Each character in the string is replaced with the characters in the opposite position in the alphabet and vice versa. (a – z, b – y, c – x, d – w, … similarly z – a, y – b, x – c …). Example: Input string – apple Encrypted string – zkkov Method – Character a is replaced by z (a is the first letter and z is the last letter) Character p is replaced by k (p is in position 16 and k is the opposite letter in position 11) Your program is also expected to decrypt the encrypted string to bring back the original content. Using C language help :((

Solutions

Expert Solution

Complete program:

#include <stdio.h>
#include <conio.h>
#include <string.h>

/*
Swapping by position.
Characters in the array are swapped with the opposite characters based
on their position in the string. Example: Input string – apple.
Encrypted string – elppa Method:
The first character ‘a’ and the last character ‘e’ – swap their position. – eppla
The second character ‘p’ and the one before the last character ‘l’ swap their position – elppa
*/

void EncryptBySwapPositions(char *inputString)
{
unsigned int index = 0;
unsigned int length = strlen(inputString);
unsigned int midIndex = length / 2;
char ch;

/*
* following loop iterates till the index reaches middle of the string
* stores the value at index in ch temporary variable
* place the value from the rear end index calculated using (length - index -1) at index
* place the value stored in ch at index (length - index -1)
*/
while (index < midIndex)
{
ch = inputString[index];
inputString[index] = inputString[length - index - 1];
inputString[length - index - 1] = ch;
index++;
}
}

void DecryptSwapPositionsString(char *inputString)
{
unsigned int index = 0;
unsigned int length = strlen(inputString);
unsigned int midIndex = length / 2;
char ch;

/*
* following loop iterates till the index reaches middle of the string
* stores the value at index in ch temporary variable
* place the value from the rear end index calculated using (length - index -1) at index
* place the value stored in ch at index (length - index -1)
*/
while (index < midIndex)
{
ch = inputString[index];
inputString[index] = inputString[length - index - 1];
inputString[length - index - 1] = ch;
index++;
}
}

/*
Change position:
Characters are moved in the forward by specific number of positions.
The shifting of the characters to be based on a circle, with the characters
in the end being inserted at the beginning of the new string.
As the first character moves one step, the last character of the string is
to be inserted as the first character of the encrypted string.
User inputs the number of positions to be moved forward.
Note: The number of positions by which the first character to be moved
cannot exceed the length of the string.
Example:
Input string – apple
Number of positions to be shifted – 3
Encrypted string - pleap
Method: The first character is moved by 3 positions –
Moved by 1 position Moved by 2 positions Moved by 3 positions
eappl leapp pleap
*/

void EncryptByChangePosition(char *inputString, int position)
{
char ch, temp;
int i = 0, j = 0;
int length = strlen(inputString);

for (i = 0; i < position; i++)
{
/* store last character */
ch = inputString[length - 1];

/* move all characters to next index to make place for last character */
for (j = length - 1; j > 0; j--)
{
inputString[j] = inputString[j - 1];
}

/* place the stored last character at first index */
inputString[0] = ch;
}
}

void DecryptChangePositionString(char *inputString, int position)
{

char ch, temp;
int i = 0, j = 0;
int length = strlen(inputString);

for (i = 0; i < position; i++)
{
/* store first character */
ch = inputString[0];

/* move all characters starting from index 1 to one place forward. 1 to 0, 2 to 1, ... */
for (j = 1; j < length; j++)
{
inputString[j - 1] = inputString[j];
}

/* place the stored first character at last index */
inputString[length - 1] = ch;
}
}

/*
Opposite Switch:
Each character in the string is replaced with the characters in the
opposite position in the alphabet and vice versa.
(a – z, b – y, c – x, d – w, … similarly z – a, y – b, x – c …).
Example:
Input string – apple
Encrypted string – zkkov
Method – Character a is replaced by z (a is the first letter and z is the last letter)
Character p is replaced by k (p is in position 16 and k is the opposite letter in position 11)
*/

void EncryptByOppositeSwitch(char *inputString)
{
int i = 0;
int length = strlen(inputString);

/* loop through each Character */
for (i = 0; i < length; i++)
{
/* replace the Character by its opposite */
inputString[i] = 'a' + 'z' - inputString[i];
}
}

void DecryptOppositeSwitchString(char *inputString)
{
int i = 0;
int length = strlen(inputString);

/* loop through each Character */
for (i = 0; i < length; i++)
{
/* replace the Character by its opposite */
inputString[i] = 'a' + 'z' - inputString[i];
}
}

int main()
{
char inputString[128] = { 0 };
int i, pos;
char exitFlag = 1;

while (exitFlag)
{
printf("Choose one of the encryption method:\n");
printf("\t1. Swap Positions (Enter 1)\n");
printf("\t2. Change Positions (Enter 2)\n");
printf("\t3. Opposite switch (Enter 3)\n");
printf("\t4. Enter -1 to exit\n");
scanf("%d", &i);

if (i >= 1 && i <= 3)
{
printf("Enter the string you want to encrypt: ");
scanf("%s", inputString);
printf("\nInput String: %s\n", inputString);
}

switch (i)
{
case -1:
printf("Exiting.\n");

exitFlag = 0;

break;
case 1:
printf("You have choosen SWAP POSITIONS encryption method.\n");

EncryptBySwapPositions(inputString);
printf("Encrypted string: %s\n", inputString);

DecryptSwapPositionsString(inputString);
printf("Decrypted string: %s\n\n", inputString);

break;
case 2:
printf("You have choosen CHANGE POSITIONS encryption method.\n");

printf("Enter the number of positions to be shifted: ");
scanf("%d", &pos);

if (pos <= strlen(inputString))
{
EncryptByChangePosition(inputString, pos);
printf("Encrypted string: %s\n", inputString);

DecryptChangePositionString(inputString, pos);
printf("Decrypted string: %s\n\n", inputString);
}
else
{
printf("Entered number of positions to be shifted is greater then length of the string!. ");
printf("Please try again.\n");
}

break;
case 3:
printf("You have choosen OPPOSITE SWITCH encryption method.\n");

EncryptByOppositeSwitch(inputString);
printf("Encrypted string: %s\n", inputString);

DecryptOppositeSwitchString(inputString);
printf("Decrypted string: %s\n\n", inputString);

break;
default:
printf("Invalid value entered. Please try again.\n");
break;
}
}

return 0;
}

Sample output:

Choose one of the encryption method:
   1. Swap Positions (Enter 1)
   2. Change Positions (Enter 2)
   3. Opposite switch (Enter 3)
   4. Enter -1 to exit
1
Enter the string you want to encrypt: apple

Input String: apple
You have choosen SWAP POSITIONS encryption method.
Encrypted string: elppa
Decrypted string: apple

Choose one of the encryption method:
   1. Swap Positions (Enter 1)
   2. Change Positions (Enter 2)
   3. Opposite switch (Enter 3)
   4. Enter -1 to exit
2
Enter the string you want to encrypt: apple

Input String: apple
You have choosen CHANGE POSITIONS encryption method.
Enter the number of positions to be shifted: 3
Encrypted string: pleap
Decrypted string: apple

Choose one of the encryption method:
   1. Swap Positions (Enter 1)
   2. Change Positions (Enter 2)
   3. Opposite switch (Enter 3)
   4. Enter -1 to exit
2
Enter the string you want to encrypt: apple

Input String: apple
You have choosen CHANGE POSITIONS encryption method.
Enter the number of positions to be shifted: 2
Encrypted string: leapp
Decrypted string: apple

Choose one of the encryption method:
   1. Swap Positions (Enter 1)
   2. Change Positions (Enter 2)
   3. Opposite switch (Enter 3)
   4. Enter -1 to exit
3
Enter the string you want to encrypt: apple

Input String: apple
You have choosen OPPOSITE SWITCH encryption method.
Encrypted string: zkkov
Decrypted string: apple

Choose one of the encryption method:
   1. Swap Positions (Enter 1)
   2. Change Positions (Enter 2)
   3. Opposite switch (Enter 3)
   4. Enter -1 to exit
- 4
Invalid value entered. Please try again.
Choose one of the encryption method:
   1. Swap Positions (Enter 1)
   2. Change Positions (Enter 2)
   3. Opposite switch (Enter 3)
   4. Enter -1 to exit
-1
Exiting.


Related Solutions

Write a C++ program to read characters from the keyboard until a '#' character is read....
Write a C++ program to read characters from the keyboard until a '#' character is read. Then the program will find and print the number of uppercase letters read from the keyboard.
Write a basic C++ program with function, whose input is a character and a string, and...
Write a basic C++ program with function, whose input is a character and a string, and whose output indicates the number of times the character appears in the string. Ex: If the input is: n Monday the output is: 1 Ex: If the input is: z Today is Monday the output is: 0 Ex: If the input is: n It's a sunny day the output is: 2 Case matters. n is different than N. Ex: If the input is: n...
To begin, write a program to loop through a string character by character. If the character...
To begin, write a program to loop through a string character by character. If the character is a letter, print a question mark, otherwise print the character. Use the code below for the message string. This will be the first string that you will decode. Use the String class method .charAt(index) and the Character class method .isLetter(char). (You can cut and paste this line into your program.) String msg = "FIG PKWC OIE GJJCDVKLC MCVDFJEHIY BIDRHYO.\n"; String decryptKey = "QWERTYUIOPASDFGHJKLZXCVBNM";...
In objective-C Task: Write a program whose input is a character and a string, and whose...
In objective-C Task: Write a program whose input is a character and a string, and whose output indicates the number of times the character appears in the string. The output should include the input character and use the plural form, n's, if the number of times the characters appears is not exactly 1.You may assume that the string does not contain spaces and will always contain less than 50 characters. Ex: If the input is: n Monday the output is:...
write a java program to Translate or Encrypt the given string : (input char is all...
write a java program to Translate or Encrypt the given string : (input char is all in capital letters) { 15 } *) Each character replaced by new character based on its position value in english alphabet. As A is position is 1, and Z is position 26. *) New characters will be formed after skipping the N (position value MOD 10) char forward. A->A+1= B , B->B+2=D ,C->C+3=F, .... Y->Y+(25%10)->Y+5=D A B C D E F G H I...
Write a program that accepts a string and character as input, then counts and displays the...
Write a program that accepts a string and character as input, then counts and displays the number of times that character appears (in upper- or lowercase) in the string. Use C++ Enter a string: mallet Enter a character: a "A" appears 1 time(s) Enter a string: Racecar Enter a character: R "R" appears 2 time(s)
Write an LC-3 program that will repeatedly read a character from the keyboard. For each character...
Write an LC-3 program that will repeatedly read a character from the keyboard. For each character read in, your program will print a neat message that echoes the input value, and the ASCII character code of the input character in hexadecimal. It will run forever: no HALT or End of processing is required. For example: Please press a key: You pressed 'z' which is x7A Please press a key: You pressed '@' which is x40 Please press a key: You...
Using the windows32 framework, write a procedure to read a string and shift each character of...
Using the windows32 framework, write a procedure to read a string and shift each character of the string by one. As an example, if your input string is Abcz, your output should be Bcda. Note that you must test your string for non-alphabetic characters (such as numbers and special characters). If there are non-alphabetic characters, you should terminate your program with an appropriate message. You should display your converted string using the output macro. Hint: Let your procedure returns an...
C++, Write a function, noPunct, that would take a string, go through the string character by...
C++, Write a function, noPunct, that would take a string, go through the string character by character, and push any character that is NOT a punctuation mark onto a stack but count the punctuation dropped. After going through the entire string, print the contents of the stack. Then print the number of punctuation dropped. Hint: use ispunct() library function (returns true if character is punctuation)
Write a program whose input is a string which contains a character and a phrase, and...
Write a program whose input is a string which contains a character and a phrase, and whose output indicates the number of times the character appears in the phrase. Ex: If the input is: n Monday the output is: 1 Ex: If the input is: z Today is Monday the output is: 0 Ex: If the input is: n It's a sunny day the output is: 2 Case matters. Ex: If the input is: n Nobody the output is: 0...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT