Question

In: Computer Science

what does this code do? int encrypt(int character, int shift) { if(character >= 'a' && character...

what does this code do?

int encrypt(int character, int shift) {
if(character >= 'a' && character <= 'z') {
return 'a' + (((character-'a'+shift) % 26) + 26) % 26 ;
} else if (character >= 'A' && character <= 'Z') {
return 'A' + (((character-'A'+shift) % 26) +26) % 26;
} else {
return character;
}
}

int main(int argc, char *argv[]) {
int ch;
int key = 3;
if(argc == 2) {
key = atoi(argv[1]);
}
while((ch = getchar()) != EOF) {
printf("%c", encrypt(ch, key));
}
return 0;
}

how does this work as I am confused about the % used.

Solutions

Expert Solution

Please find the following explanation on given program.

Explanation:

1. This program encrypts the character by using the given key which shifts character based it. If it goes beyond 'z' then it wraps around and starts from 'a'.

2. Here "%" is a modulo operator which returns the reminder of a division.

3. In api we have a hard coded value which is 26 which represents that there are 26 alphabets.

4. Following statement receives our input character, say 'b' and shifts based on given key value,

return 'a' + (((character-'a'+shift) % 26) + 26) % 26 ;

='a' + ((('b' - 'a' + 3) % 26) + 26) %26

='a' + (((1+3)% 26) + 26 ) % 26

= 'a' + (4 + 26) % 26

='a' + 30 % 26

=61 + 4

= 'a' ascii value is  61 so (61+4) retruns 65 which is character 'e'

Sample Program:

#include <stdio.h>
#include <stdlib.h>

int encrypt(int character, int shift) {
if(character >= 'a' && character <= 'z') {
//return 'a' + (((character-'a'+shift) % 26) + 26) % 26 ;
return 'a' + (((character-'a'+shift) % 26) ); // we can also use this statement to shift the character.
} else if (character >= 'A' && character <= 'Z') {
return 'A' + (((character-'A'+shift) % 26) +26) % 26;
} else {
return character;
}
}

int main(int argc, char *argv[]) {
int ch;
int key = 3;
if(argc == 2) {
key = atoi(argv[1]);
}
while((ch = getchar()) != EOF) {
printf("%c", encrypt(ch, key));
}
return 0;
}


Related Solutions

Arduino Uno code, Please explain what does this code do? unsigned long ms_runtime; int one_ms_timer; //define...
Arduino Uno code, Please explain what does this code do? unsigned long ms_runtime; int one_ms_timer; //define all timers as unsigned variables unsigned long timer1 = 0; // timer1 increments every 100ms = 0.1s const int USER_LED_OUTPUT = 13; const int USER_BUTTON_INPUT = 2; void setup() { // initialize the digital pin as an output. pinMode(USER_LED_OUTPUT, OUTPUT); pinMode(USER_BUTTON_INPUT, INPUT); Serial.begin(9600); } void loop() { // run loop forever static int state, old_state,counter; timers(); // logic for state change if(state == 0)...
C CODE PLZ! All instructions for what to do in code #include <stdio.h> int main(int argc,...
C CODE PLZ! All instructions for what to do in code #include <stdio.h> int main(int argc, char **argv) { int n, k, l, r, t, d, i; char str[65]; /* Make the user enter a non-negative integer */ printf("Please enter a non-negative integer: "); scanf("%d", &n); while (n < 0) { printf("Sorry, your input is incorrect.\n"); printf("Please enter a non-negative integer: "); scanf("%d", &n); } /* Convert the integer to reversed binary: e.g. 6 gets converted to 011 */ if...
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...
Convert this C++ code to Java code this code is to encrypt and decrypt strings of...
Convert this C++ code to Java code this code is to encrypt and decrypt strings of characters using Caesar cipher please attach samples run of the code #include <iostream> #include <stdio.h> #include <ctype.h> #include <stdlib.h> #include <string> #define MAX_SIZE 200 void encrypt_str(char xyz[], int key); // Function prototype for the function to encrypt the input string. void decrypt_str(char xyz[], int key); // Function prototype for the function to decrypt the encrypted string. using namespace std; int main() { char input_str[MAX_SIZE];...
please explain how does the following C code work. a) int function1(int num1, int num2) {...
please explain how does the following C code work. a) int function1(int num1, int num2) { int num = num1 ^ num2; int result = 0; while(num > 0) { result += (num & 1); num >>= 1; } return result; } b) int function2(unsigned int num) { return num & ~(num - 1); } c) int f1(unsigned int x) { int count = 0; while(x) { count++; x = x&(x-1); } return count; } d) double ldexp(double x, int...
Desc: Encrypts or decrypts a file. Input: The user supplies the character '1' to encrypt, or...
Desc: Encrypts or decrypts a file. Input: The user supplies the character '1' to encrypt, or '2' to decrypt via the keyboard.   The user also supplies the name of the source file via the keyboard.   Output: If the user wants to encrypt, the text in input file is encrypted and the encrypted text is stored in "encrypted.txt". The original file is not changed. If the user wants to decrypt, the text in input file is decrypted and the decrypted text...
1)What is the output of the following code? struct someType { int a; int b; };...
1)What is the output of the following code? struct someType { int a; int b; }; void f1(someType &s) { s.a = 1; s.b = 2; } someType f2(someType s) { someType t; t = s; s.a = 3; return t; } int main() { someType s1, s2; f1(s1); s2 = f2(s1); cout << s1.a << '-' << s1.b << '-' << s2.a << '-' << s2.b << '-' << endl; return 0; } ------------------------------------------------------------------------------------------------------------------ 2) struct dateType { int...
Can someone explain the code for each line?(how do they run,what do they mean) void printArray2D(int...
Can someone explain the code for each line?(how do they run,what do they mean) void printArray2D(int arr[M][N]){ int i, j; for(int i =0; i < M; i++){ for(int j=0; j<N; j++){ printf(" %d ", arr[i][j]);//print all the array elements } printf("\n"); } } void populateRandom2D(int arr[M][N]){ int i, j; int min, max; printf("Enter min number in the array:\n"); scanf("%d", &min); printf("Enter max number in the array:\n"); scanf("%d", &max); for(int i =0; i < M; i++){ for(int j=0; j< N; j++){...
What is the ouput of the following code? void loop(int num) { for(int i = 1;...
What is the ouput of the following code? void loop(int num) { for(int i = 1; i < num; ++i) { for(int j = 0; j < 5; ++j) { cout << j; } } } int main() { loop(3); return 0; }
What is the output of the following C++ code? int* length; int* width; length = new...
What is the output of the following C++ code? int* length; int* width; length = new int; *length = 5; width = length; length = new int; *length = 2 * (*width); cout << *length << " " << *width << " " << (*length) * (*width) << endl;
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT