Question

In: Computer Science

. A simple way to encrypt a number is to replace each digit of the number...

. 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 */

Solutions

Expert Solution

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


Related Solutions

Python 3 A simple way to encrypt a file is to change all characters following a...
Python 3 A simple way to encrypt a file is to change all characters following a certain encoding rule. In this question, you need to move all letters to next letter. e.g. 'a'->'b', 'b'->'c', ..., 'z'->'a', 'A'->'B', 'B'->'C', ..., 'Z'->'A'. For all digits, you need to also move them to the next number. e.g. '0'->'1', '1'->'2', ..., '9'->'0'. All the other symbols should not be changed. Write a function encrypt with the following requirements: the function takes a string argument,...
The number 52430 is a 5-digit number whereas 03201 is not a 5-digit number. Determine the...
The number 52430 is a 5-digit number whereas 03201 is not a 5-digit number. Determine the number of 5-digit multiples of 5 that can be created using the digits 0 to 9, if digits may not be repeated
Credit Card Number Check. The last digit of a credit card number is the check digit,...
Credit Card Number Check. The last digit of a credit card number is the check digit, which protects against transcription errors such as error in a single digit or switching two digits. The following method is used to verify actual credit card number but, for simplicity, we will describe it for numbers with 8 digits instead of 16: Starting from the rightmost digit, form the sum of every other digit. For example, if the credit card number is 43589795, then...
Credit Card Number Check. The last digit of a credit card number is the check digit,...
Credit Card Number Check. The last digit of a credit card number is the check digit, which protects against transcription errors such as an error in a single digit or switching two digits. The following method is used to verify actual credit card numbers but, for simplicity, we will describe it for numbers with 8 digits instead of 16: • Starting from the rightmost digit, form the sum of every other digit. For example, if the credit card number is...
Credit Card Number Check. The last digit of a credit card number is the check digit,...
Credit Card Number Check. The last digit of a credit card number is the check digit, which protects against transcription errors such as an error in a single digit or switching two digits. The following method is used to verify actual credit card numbers but, for simplicity, we will describe it for numbers with 8 digits instead of 16: • Starting from the rightmost digit, form the sum of every other digit. For example, if the credit card number is...
Gather three close friends and conduct this simple experiment. Mention a 10-digit code number and allow...
Gather three close friends and conduct this simple experiment. Mention a 10-digit code number and allow them to memorize in 5 minutes. After five minutes let each person mention the 10-digit code number. Repeat experiment and let each person mention the code after 20 minutes i) what were your observations? Explain how this relate to short-term memory span in human beings.
Program 5: The concept of a 5-digit palindromenumber is a 5-digit number that reads the...
Program 5: The concept of a 5-digit palindrome number is a 5-digit number that reads the same from left to right and from right to left. For example, 12121, 45454, and 14741 are valid 5-digit palindrome numbers. Design (pseudocode) and implement (source code) a program (name it FiveDigitPalindrom) that reads a 5-digit number from the user (as integer value, not string) and then mathematically (using division and remainder operations) determines whether the entered number is a 5-digit palindrome or not....
Suppose that a check digit is assigned to a four-digit number by appending the remainder after...
Suppose that a check digit is assigned to a four-digit number by appending the remainder after division by 7. If the number 36806 has a single-digit error in the first position, determine the possibilities for the correct number.
Java Code!!!! A five-digit number is said to be friendly if: the leftmost digit is divisible...
Java Code!!!! A five-digit number is said to be friendly if: the leftmost digit is divisible by 1 and the leftmost two digits are divisible by 2 and the leftmost 3 digits are divisible by 3 and the leftmost 4 digits are divisible by 4 and leftmost 5 digits (the five-digit number itself) is divisible by 5. For example, the number 42325 is a friendly number: 4 is divisible by 1 and 42 is divisible by 2 and 423 is...
Write a java program The last digit of a credit card number is the check digit,...
Write a java program The last digit of a credit card number is the check digit, which protects againts transaction errors. The following method is used to veryfy credit card numbers. For the simplicity we can assume that the credit card has 8 digits instead of 16. Follwing steps explains the algorithm in determining if a credit card number is a valid card.  Starting from the right most digit, form the sum of every other digit. For example, if...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT