In: Computer Science
. A simple way to encrypt a number is to replace each digit of the number with another digit. Write a C program that asks the user to enter a positive integer and replace each digit by adding 6 to the digit and calculate the remainder by 10. For example, if a digit is 6, then the digit is replaced by (6+6)%10, which is 2. After all the digits are replaced, the program then swap the first digit with the last digit. The main function reads in input and displays output.
A sample input/output:
Enter the number of digits of the input number: 3
Enter the number: 728
Output: 483
2) The user will enter the total number of digits before entering the number.
3) You can use format specifier "%1d" in scanf to read in a single digit into a variable (or an array element). For example, for input 101011, scanf("%1d", &num) will read in 1 to num.
4) As part of the solution, write and call the function encrypt() with the following prototype.
The function assumes that the digits are stored in the array a and computes the encrypted digits and store them in the array b. c represents the size of the arrays. void encrypt(int *a, int *b, int n); The function should use pointer arithmetic – not subscripting – to visit array elements. In other words, eliminate the loop index variables and all use of the [] operator in the function.
5) As part of the solution, write and call the function swap() with the following prototype. void swap(int *p, int *q);
When passed the addresses of two variables, the swap() function should exchange the values of the variables: swap(&i, &j); /* exchange values of i and j */
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks
#include<stdio.h>
//method to swap values of two pointers
void swap(int *p, int *q){
//storing value of p in temp
int temp=*p;
//assigning value of q to p
*p=*q;
//assigning stored value to q
*q=temp;
}
//method to encrypt elements in array a using given technique
void encrypt(int *a, int *b, int n){
//recording starting address of b
int* b_start=b;
//recording address of last element in a
int* a_end=a+n-1;
//looping as long as a is within the range
while(a<=a_end){
//getting value from pointer a
int value=*a;
//finding encrypted digit
value=(value+6)%10;
//storing encrypted value in current location pointed by b
*b=value;
//advancing pointers a and b to next location
a++;
b++;
}
//now swapping values at pointers b_start and b-1, here b-1 is the address of
//last element in second array, because after the loop, b is now pointing to the
//location after the last element
swap(b_start,b-1);
}
int main(){
//initializing variables, assuming number of digits is never more than 20
int n, arr[20], enc[20];
printf("Enter the number of digits of the input number: ");
//reading number of digits
scanf("%d",&n);
printf("Enter the number: ");
//reading n digits and storing in arr
for(int i=0;i<n;i++){
scanf("%1d",&arr[i]);
}
//encrypting arr to store enc
encrypt(arr,enc,n);
//printing elements in enc
printf("Output: ");
for(int i=0;i<n;i++){
printf("%d",enc[i]);
}
printf("\n");
return 0;
}
/*OUTPUT*/
Enter the number of digits of the input number: 3
Enter the number: 728
Output: 483