In: Computer Science
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 :((
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.