In: Computer Science
C Programming Language Please
Given a string S and keyword string K, encrypt S using keyword Cipher algorithm.
Note: The encryption only works on alphabets. Numbers and symbols such as 1 to 9, -, &, $ etc remain unencrypted.
Input:
Zombie Here
secret
where:
Output:
ZLJEFT DTOT
Explanation: We used "secret" keyword there.
Here I am solving above encryption problem in C language.see below.
encrypted_string.c
#include<stdio.h>
#include<string.h>
int main(){
char msg[10000],key[100];
printf("Enter message:
"); // taking input message for
encryption
gets(msg);
printf("Enter keyword:
"); // taking key
as input here
gets(key);
strupr(key);
// converting to upper case
strupr(msg);
// both msg and key
char alphabet[26];
int ind=0,i;
for(i=65;i<91;i++)
{
// taking alphabets in UPPER case
char c=i;
alphabet[i-65]=c;
}
char encrypted[10000];
strcpy(encrypted,key);
strcat(encrypted,alphabet); // merging alphabets and
encrypted key
//printf("%s",encrypted);
int k=0,j;
// converting to encrypted one by removing duplicates
in string
char c='*';
for(i=0;encrypted[i];i++)
{
if(!(encrypted[i]==c))
{
for(j=i+1;encrypted[j];j++)
{
if(encrypted[i]==encrypted[j])
encrypted[j]=c;
}
}
}
for(i=0;encrypted[i];i++)
{
encrypted[i]=encrypted[i+k];
if(encrypted[i]==c)
{
k++;
i--;
}
}
// printing resultant encrypted string.
for(i=0;i<strlen(msg);i++)
{
if(isalpha(msg[i])) // checking
only alphabets
{
int
index=(msg[i])-65;
printf("%c",encrypted[index]); // printing each
character
}
else
printf("%c",msg[i]);
}
return 0;
}
Code - Images:-
output:-
Thank you..!! Please do upvote..!!