Advertisement
Guest User

Untitled

a guest
Nov 27th, 2018
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.59 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. #include <stdlib.h>
  4.  
  5. #include <string.h>
  6.  
  7. #define MAX 100
  8.  
  9. int scegli_opzione(void);/*OK*/
  10.  
  11. int alloca_array_char(char **, int *); /* WORK IN PROGRES... */
  12.  
  13. int ricevi_frase(char [] [MAX]);/*OK*/
  14.  
  15. void encrypt(char *, char []);
  16.  
  17. void sostituisci(char *, char []); /*QUESTA FUNZIONE SCORRE LA STRINGA CERCANDO UN ELEMENTO = ALL'ELEMENTO DELLA CHIAVE, SE TROVA UNA CORRISPONDEZA, PONE ELEMENTO DELLA STRINGA CORRENTE = ALL'ELEMENTO DELLA CHIAVE CORRISPONDENTE*/
  18.  
  19. void ricevi_stringa(char *);/*OK*/
  20.  
  21. void ricevi_intero(float *); /*OK*/
  22.  
  23. int main(void){
  24.  
  25. char frase[MAX][MAX], *key = "l98goA7r34J1tXYRIwdFuzEQ26";
  26.  
  27. int parole_tot, i;
  28.  
  29. printf("Questo programma riceve in input una frase e la cripta secondo una chiave crittografica. \n");
  30.  
  31. while(scegli_opzione()){
  32.  
  33. parole_tot = ricevi_frase(frase);
  34.  
  35. /*printf("\n %s", *(frase + 2));*/
  36.  
  37. for(i = 0; i < parole_tot; i++){
  38.  
  39. encrypt(frase[i], key);
  40.  
  41. printf("\n %s \n", *(frase + i));
  42. }
  43. }
  44.  
  45. printf("\n LA CHIAVE ERA %s", key);
  46.  
  47. return 0;
  48. }
  49.  
  50. int ricevi_frase(char frase[] [MAX]){ /* OK,MA POTENZIALMENTE WORK IN PROGRESS */
  51.  
  52. float n_parole;
  53.  
  54. int i;
  55.  
  56. printf("\nInserisci il numero di parole della frase: ");
  57.  
  58. ricevi_intero(&n_parole);
  59.  
  60. for(i = 0; i < n_parole; i++){
  61.  
  62. ricevi_stringa(frase[i]); /* frase[i] corrisponde all'indirizzo base della i-esima riga */
  63.  
  64. }
  65.  
  66. return n_parole;
  67.  
  68. }
  69.  
  70. void ricevi_stringa(char *str){ /* OK,MA POTENZIALMENTE WORK IN PROGRESS */
  71.  
  72. printf("Inserisci stringa: ");
  73.  
  74. scanf("%s", str);
  75.  
  76. }
  77.  
  78. void encrypt(char *str, char *key){ /* OK */
  79.  
  80. int i;
  81.  
  82. for(i = 0; i < strlen(str); i++){
  83.  
  84. sostituisci(str + i, key);
  85. }
  86.  
  87. }
  88.  
  89. void sostituisci(char *lettera, char key[]){ /* OK */
  90.  
  91. char *alfabeto = "qwertyuiopasdfghjklzxcvbnm"; int i;
  92.  
  93. for(i = 0; i < strlen(alfabeto); i++){ /* QUESTO CICLO, ASSEGNA, QUANDO TROVA UNA CORRISPONDENZA TRA IL VALORE CONTENUTO
  94. * NELL'INDIRIZZO LETTERA E UN ELEMENTO DELLA STRINGA ALFABETO, IL CORRISPETTIVO VALORE DELLA STRINGA KEY */
  95. if(*lettera == *(alfabeto + i)){
  96.  
  97. *lettera = *(key + i);
  98.  
  99. break;
  100. }
  101. }
  102. }
  103.  
  104. void ricevi_intero(float *n){ /* OK */
  105.  
  106. int check_n;
  107.  
  108. printf("\nInserisci un numero intero positivo: ");
  109.  
  110. scanf("%f", n);
  111.  
  112. check_n = (int) *n;
  113.  
  114. while(check_n < *n || n < 0){
  115.  
  116. printf("\n Hai sbagliato! Inserisci un intero positivo!\n");
  117.  
  118. printf("\nInserisci un numero intero positivo: ");
  119.  
  120. scanf("%f", n);
  121.  
  122. check_n = (int) *n;
  123. }
  124.  
  125. *n = (int)*n;
  126. }
  127.  
  128. int scegli_opzione(void){ /* OK */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement