all in c Write a program in C which reads a text from the keyboard and calculate
ID: 3703738 • Letter: A
Question
all in c
Write a program in C which reads a text from the keyboard and calculates (prints in percentage and in number) how many times each letter was typed. Capital and lower letters should be considered the same. Be as efficient as possible! Exercise 3 - Encryption and decryption of a text A simple cryptography method is the following one (Vigenere cipher): . We take the text we want to encrypt and the key P. We suppose that P has length equal to n. . We assign to each letter, both for the text and the key, the corresponding number (a 1, 2, c = 3, d = 4, ,z-26) .Encoding: We take the first letter of the text and the first letter of the key and we add their r representations. Then we find which character corresponds to the sum and we replace first character of the text. Then we take the second character of the text and the second character of the key and we do the same etc. If the sum is above 26, we sub 26. Decoding: We take the first letter of the encoded text and the first letter of the key and we subtract their number representations. Then we find which character corresponds to the result and we replace the first character of the encoded text. Then we take the second character of the encoded text and the second character of the key etc. If the result is below 0, we add 26. . In any case, if the key ends, we start from the beginning.Explanation / Answer
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <limits.h>
void encrypt(char *str, char *key, char *result, int len){
int i, keylen;
keylen=strlen(key);
for(i=0;i<len;i++)
{
int x;
if(islower(str[i]))
{
if((str[i]-'a')+(key[i%keylen]-'a'+2)>26)
x=str[i]+(key[i%keylen]-'a'+1)-26;
else
x=str[i]+(key[i%keylen]-'a'+1);
}
else
{
if((str[i]-'A'+1)+(key[i%keylen]-'a'+1)>26)
x=str[i]+(key[i%keylen]-'a'+1)-26;
else
x=str[i]+(key[i%keylen]-'a'+1);
}
result[i]=x;
}
}
void decrypt(char *str, char *key, int len){
char result[len];
int i, keylen;
keylen=strlen(key);
for(i=0;i<len;i++)
{
int x;
if(islower(str[i]))
{
if((str[i]-'a')-(key[i%keylen]-'a'+1)<0)
x=str[i]-(key[i%keylen]-'a'+1)+26;
else
x=str[i]-(key[i%keylen]-'a'+1);
}
else
{
if((str[i]-'A')-(key[i%keylen]-'a'+1)<0)
x=str[i]-(key[i%keylen]-'a'+1)+26;
else
x=str[i]-(key[i%keylen]-'a'+1);
}
result[i]=x;
}
printf("%s",result);
}
int main(){
char str[]="Chegg", key[]="abc";
/*
printf("Enter the phrase: ");
scanf("%s",str);
printf("Enter the key: ");
scanf("%s",key);
*/
int len=strlen(str);
char result[len];
printf("key: %s",key);
printf(" Original text: %s",str);
encrypt(str, key, result, len);
printf(" Encrypred text is: %s",result);
printf(" Decrypted text is: ");
decrypt(result, key, len);
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.