Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /******************************************************************************
- Online C Compiler.
- Code, Compile, Run and Debug C program online.
- Write your code in this editor and press "Run" button to compile and execute it.
- *******************************************************************************/
- #include <stdio.h>
- #include <string.h>
- // A B C ..... Z
- // 65 66 67 ..... 90
- // 75 76 77 ..... 100
- char
- decr (char c, int k)
- {
- char suma = c + k;
- if (suma > 'Z')
- return 'A' + (suma % 'Z') - 1;
- else
- return (char) suma;
- }
- void
- decripteaza (char *text, int k)
- {
- printf ("Cheia initiala %d:\n", (26 - k) % 26);
- for (int i = 0; i < strlen (text); i++)
- {
- if (text[i] == ' ')
- printf ("%c", text[i]);
- else
- printf ("%c", decr (text[i], k));
- }
- printf ("\n\n");
- }
- // ex2
- void
- set_corespondente (char *ptr, char c1, char c2)
- {
- ptr[c1] = c2;
- }
- void
- print_text (char *ptr, char *text)
- {
- printf ("Textul descifrat: \n");
- for (int i = 0; i < strlen (text); i++)
- if (text[i] == ' ')
- printf ("%c", text[i]);
- else
- printf ("%c", ptr[text[i]]);
- }
- int frecvente[26];
- typedef struct
- {
- char litera;
- int frecventa;
- } dictionar;
- void initdict(dictionar *d)
- {
- for(int i = 0; i < 26; i++)
- {
- d[i].litera = 'A' + i;
- d[i].frecventa = 0;
- }
- }
- void setdict(dictionar *d, char c, int frecventa)
- {
- int pozitie = c - 'A';
- d[pozitie].frecventa = frecventa;
- }
- void printdict(dictionar *d){
- for(int i = 0; i < 26; i++)
- printf("Caracterul %c: %d\n",d[i].litera,d[i].frecventa);
- }
- void swap(dictionar *d1, dictionar *d2){
- dictionar temp = *d1;
- *d1 = *d2;
- *d2 = temp;
- }
- void orderdict(dictionar *d){
- for(int i = 0; i < 26; i++)
- for(int j = 0; j < 26; j++)
- if(d[i].frecventa > d[j].frecventa)
- swap(&d[i],&d[j]);
- }
- int
- main ()
- {
- // exercitiul 1
- /*char *text = "ESPCP LCP LWDZ WPEEPCD ZQ STD EZ NTNPCZ LD HPWW LD EZ STD TYETXLEPD ZY ACTGLEP LQQLTCD LYO TY ESP WLEEPC TQ SP SLO LYJESTYR NZYQTOPYETLW EZ DLJ SP HCZEP TE TY NTASPC ESLE TD MJ DZ NSLYRTYR ESP ZCOPC ZQ ESP WPEEPCD ZQ ESP LWASLMPE ESLE YZE L HZCO NZFWO MP XLOP ZFE TQ LYJZYP HTDSPD EZ OPNTASPC ESPDP LYO RPE LE ESPTC XPLYTYR SP XFDE DFMDETEFEP ESP QZFCES WPEEPC ZQ ESP LWASLMPE, YLXPWJ O QZC L LYO DZ HTES ESP ZESPCD ESP WTGPD ZQ ESP NLPDLCD WTQP ZQ UFWTFD NLPDLC";
- for(int i = 0; i < 26; i++)
- decripteaza(text,i);
- */
- // exercitiul 2
- char *text =
- "KBH XSQXKZKSKZNF DZCBHTX THCMADH HADB MHKKHT IZKB AFNKBHT MHKKHT ADDNTPZFE KN KBH DZCBHT AMCBAQHK DZCBHTX ZF IBZDB KBH DZCBHT AMCBAQHK THUAZFX SFDBAFEHP KBTNSEBNSK KBH UHXXAEH ATH DAMMHP UNFNAMCBAQHKZD XSQXKZKSKZNF DZCBHTX ZO IH CHTUZK KBH DZCBHT AMCBAQHK KN QH AFR THATTAFEHUHFK NO KBH CMAZF AMCBAQHK KBHF IH DAF EHFHTAKH AF HFNTUNSX FSUQHT NO PZXKZFDK UNPHX NO HFDTRCKZNF HADB DZCBHT AMCBAQHK ZX GFNIF AX A GHR ZO NST UHXXAEH ZX ZFKHTDHCKHP QR KBH HFHUR IBN DNTTHDKMR AXXSUHX KBAK IH BAWH SXHP A UNFNAMCBAQHKZD XSQXKZKSKZNF DZCBHT KBHR ATH XKZMM OADHP IZKB KBH ZUCNXXZQMH DBAMMHFEH NO DBHDGZFE AMM CNXXZQMH GHRX ZO AF HFHUR AEHFK DNSMP DBHDG NFH NO KBHXH CNXXZQMH GHRX HWHTR XHDNFP ZK INSMP KAGH TNSEBMR NFH QZMMZNF KZUHX KBH MZOHKZUH NO KBH SFZWHTXH KN DBHDG AMM NO KBHU AFP OZFP KBH DNTTHDK NFH KBZX XZUCMH QTSKH ONTDH ACCTNADB DMHATMR IZMM FNK INTG KBH QMADG DBAUQHT XZUNF XZFEB";
- printf("\n\nTextul cifrat:\n%s\n\n",text);
- char corespondente[26];
- char *ptr_corespondente = corespondente - 'A';
- int *ptr_frecvente = frecvente - 'A';
- for (int i = 0; i < strlen (text); i++){
- if (text[i] != ' '){
- ptr_frecvente[text[i]]++;
- }
- }
- dictionar d[26];
- initdict(&d);
- for (char c = 'A'; c <= 'Z'; c++){
- setdict(&d,c,ptr_frecvente[c]);
- }
- printdict(&d);
- orderdict(&d);
- printf("\n\nDictionarul sortat:\n");
- printdict(&d);
- for (char c = 'A'; c <= 'Z'; c++){
- ptr_corespondente[c] = '*';
- }
- // https://www.online-utility.org/text/analyzer.jsp - frecv. cuvintelor
- // "THE" din "KBH" (apare de 12 ori)
- set_corespondente (ptr_corespondente, 'K', 'T');
- set_corespondente (ptr_corespondente, 'B', 'H');
- set_corespondente (ptr_corespondente, 'H', 'E');
- // "LETTER" din "*ETTE*"
- set_corespondente (ptr_corespondente, 'M', 'L');
- set_corespondente (ptr_corespondente, 'T', 'R');
- // "REPLACE" din "RE*L**E"
- set_corespondente(ptr_corespondente, 'C', 'P');
- set_corespondente(ptr_corespondente, 'A', 'A');
- set_corespondente(ptr_corespondente, 'D', 'C');
- // "ANOTHER" din "A**THER"
- set_corespondente(ptr_corespondente, 'F', 'N');
- set_corespondente(ptr_corespondente, 'N', 'O');
- // "THROUGHOUT" din "THRO**HO*T"
- set_corespondente(ptr_corespondente, 'S', 'U');
- set_corespondente(ptr_corespondente, 'E', 'G');
- // "SUBSTITUTION" din "*U**T*TUT*ON"
- set_corespondente(ptr_corespondente, 'X', 'S');
- set_corespondente(ptr_corespondente, 'Q', 'B');
- set_corespondente(ptr_corespondente, 'Z', 'I');
- // "CHECKING" din "CHEC*ING"
- set_corespondente(ptr_corespondente, 'G', 'K');
- // "WITH" din "*ITH"
- set_corespondente(ptr_corespondente, 'I', 'W');
- // "UNCHANGED" din "UNCHANGE*"
- set_corespondente(ptr_corespondente, 'P', 'D');
- // "HAVE" din "HA*E"
- set_corespondente(ptr_corespondente, 'W', 'V');
- // "ENCRYPTION" din "ENCR*PTION"
- set_corespondente(ptr_corespondente, 'R', 'Y');
- // "REARRANGEMENT" din "REARRANGE*ENT"
- set_corespondente(ptr_corespondente, 'U', 'M');
- // "FACED" din "*ACED", "OF" din "O*"
- set_corespondente(ptr_corespondente, 'O', 'F');
- //GATA!
- print_text(ptr_corespondente, text);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement