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...
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...
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;
I'm having trouble understanding the following code (a snippet of a code). What does it do?...
I'm having trouble understanding the following code (a snippet of a code). What does it do? The whole code is about comparing efficiencies of different algorithms. def partition(list,first,last): piv = list[first] lmark = first+1 rmark = last done = False while not done: while lmark <= rmark and list[lmark]<=piv: lmark=lmark+1 while list[rmark]>=piv and rmark>=lmark: rmark=rmark-1 if rmark<lmark: done = True else: temp = list[lmark] list[lmark]=list[rmark] list[rmark]=temp temp = list[first] list[first]=list[rmark] list[rmark]=temp return rmark
How do I write a C# and a C++ code for creating a character array containing...
How do I write a C# and a C++ code for creating a character array containing the characters 'p', 'i', 'n','e','P','I','N','E' only and then using these lower and capital case letter character generate all possible combinations like PInE or PinE or PIne or PINE or piNE etc. and only in this order so if this order is created eg. NeIP or EnPi or NeIP or IPnE and on. You can generate all the combinations randomly by creating the word pine...
Explain the following code. What language? What does it do? What is the result?                 <!DOCTYPE...
Explain the following code. What language? What does it do? What is the result?                 <!DOCTYPE teo[                                 <ELEMENT teo((stations| databases)+)>                                 <ELEMENT stations(stationName stationLocation sensors*)> <ELEMENT databases(databaseName databaseType )> <ELEMENT stationName(#PCDATA)> <ELEMENT stationLocation(#PCDATA)> <ELEMENT sensors (sensorName phenomenon)+> <ELEMENT sensorName(#PCDATA)> <ELEMENT phenomenon(#PCDATA)> <ELEMENT databaseName(#PCDATA)> <ELEMENT databaseType(#PCDATA)> <!ATTLIST stations boundingBox CDATA #required> ]>
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT