Write a function encrypt that takes two strings \"s1\" and \"s2\" and an integer
ID: 2082017 • Letter: W
Question
Write a function encrypt that takes two strings "s1" and "s2" and an integer "shift" as parameters. Your function encrypts s1 and stores it in s2. The encryption is done by shifting each character "shift" number of places. For example if shift is 4, then every A in the s1 becomes E in s2, and B becomes F and so on. Z becomes the character 'Z' + 4 which is^according to the ASCII table. Write another similar function decrypt (that takes two strings "s1" and "s2" and an integer "shift" as parameters) and decrypts s1 by shifting its characters back "shift" places and storing it in s2. Both functions should take s1 and s2 as char*parameter (pointer to char). Write a program encryption.c that reads a sentence from the user and encrypts it then decrypts it. Your program should ask the user to enter a sentence and an integer denoting the shift number. Your program should then encrypt the string and output the encrypted result. Then it decrypts the result and outputs it to the standard output.Explanation / Answer
thanks for choosing chegg! here's the code..
#include <stdio.h>
#include <ctype.h>
#include<string.h>
#include<conio.h>
#define MAXSIZE 1024
void encrypt(char*,char*,int);
void decrypt(char*,char*,int);
int main(void)
{
char s1[MAXSIZE],s2[MAXSIZE],x[MAXSIZE];
int shift;
printf("enter the shift ");
scanf("%d",&shift);
puts("Input text to encrypt->");
// gets(s1);
// fgets(s1,10,stdin);
scanf("%s",&s1);
encrypt(s1,s2,shift);
return 0;
}
void encrypt(char*str,char* s2, int shift)
{
char *p=str,crypt[MAXSIZE],aa;
int k=0;
while(*p)
{
crypt[k]=*p+shift;
k++; p++;
}
crypt[k]='';
printf(" Encrypted string is [%s]",crypt);
s2=crypt;
decrypt(s2,s2,shift);
}
void decrypt(char*str,char*s2,int shift)
{
char *p=str,crypt[MAXSIZE],aa;
int k=0;
while(*p)
{
crypt[k]=*p-shift;
k++; p++;
}
crypt[k]='';
printf(" Dencrypted string is [%s]",crypt);
s2=crypt;
}
sometimes gets may not work depending on your compiler then u can use the scanf or the fgets function alternatively.. where fgets takes the variable name size and stdin as the inputs... whereas gets takes only the variable name as input. the problem with scanf is it doesn't take a sentence as input.. u can use
Happy learning!! :)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.