The purpose of this application is to encrypt an integer number into a text message (string).
The following replacements of digits should be made in the encryption process: $$0\rightarrow ’a’, 1 \rightarrow ’b’, 2 \rightarrow ’c’, 3 \rightarrow ’d’, 4 \rightarrow ’e’, 5 \rightarrow ’f’, 6 \rightarrow ’g’, 7 \rightarrow ’h’, 8 \rightarrow ’i’, 9 \rightarrow ’j’$$.
- Write the function $$\texttt{void EnCrypt(int n, char Encr[])}$$ that generates the encrypted value of n in the string Encr (Ex: if n = 1092 then Encr = “bajc”).
- Write a main program that prompts the user to enter an integer value and print the equivalent encryption string.
Difficulty level
This exercise is mostly suitable for students
#include <stdio.h>
#include <string.h>
void EnCrypt(int n, char Encr[])
{
int i=0,j;
char aux;
while(n)
{
Encr[i++]=n%10+'a';
n/=10;
}
Encr[i]='\0';
j=i-1;
i=0;
while(i<j)
{
aux=Encr[i];
Encr[i]=Encr[j];
Encr[j]=aux;
i++;
j--;
}
}
int main()
{
char str[100];
EnCrypt(1092,str);
printf("%s\n",str);
return 0;
}
Back to the list of exercises
Looking for a more challenging exercise, try this one !!
Sorting using merge-sort algorithm