fork download
  1. //gcc 5.4.0
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5.  
  6. char * cifra_de_cesar_encode(char * str, int deslocamento)
  7. {
  8. int i = 0;
  9.  
  10. while ((int)str[i] != 0)
  11. {
  12. str[i] = (char)((int)str[i] + (deslocamento%26));
  13. i++;
  14. }
  15. return str;
  16. }
  17.  
  18. char * cifra_de_cesar_decode(char * str, int deslocamento)
  19. {
  20. int i = 0;
  21.  
  22. while ((int)str[i] != 0)
  23. {
  24. str[i] = (int)str[i] - (deslocamento%26);
  25. i++;
  26. }
  27. return str;
  28. }
  29.  
  30. char * cifra_monoalfabetica_encode(char * str, char * alfabeto_encode, char * alfabeto_decode)
  31. {
  32. int i = 0;
  33.  
  34. while ((int)str[i] != 0)
  35. {
  36. int k = 0;
  37. while (str[i] != alfabeto_encode[k])
  38. k++;
  39. str[i] = alfabeto_decode[k];
  40. i++;
  41. }
  42. return str;
  43. }
  44.  
  45. char * cifra_monoalfabetica_decode(char * str, char * alfabeto_encode, char * alfabeto_decode)
  46. {
  47. int i = 0;
  48.  
  49. while ((int)str[i] != 0)
  50. {
  51. int k = 0;
  52. while (str[i] != alfabeto_decode[k])
  53. k++;
  54. str[i] = alfabeto_encode[k];
  55. i++;
  56. }
  57. return str;
  58. }
  59.  
  60. int main(void)
  61. {
  62. char palavra[] = "bemvindosafurbnaoesquecadefazeroexercicioapresentadopeloprofessoremsaladeaula";
  63. printf("Cifra de César:\n\n");
  64. printf("%s", palavra);
  65. const int CHAVE = 500;
  66. cifra_de_cesar_encode(palavra, CHAVE);
  67. printf("\n%s", palavra);
  68. cifra_de_cesar_decode(palavra, CHAVE);
  69. printf("\n%s", palavra);
  70. printf("\n---------------\n");
  71. printf("Cifra de Monoalfabética:\n\n");
  72. const char alfabeto_encode[] = "qazwsxedcrfvtgbyhnujmikolp";
  73. const char alfabeto_decode[] = "abcdefghijklmnopqrstuvwxyz";
  74. cifra_monoalfabetica_encode(palavra, alfabeto_encode, alfabeto_decode);
  75. printf("%s", palavra);
  76. cifra_monoalfabetica_decode(palavra, alfabeto_encode, alfabeto_decode);
  77. printf("\n%s", palavra);
  78. return 0;
  79. }
  80.  
Success #stdin #stdout 0s 9432KB
stdin
Standard input is empty
stdout
Cifra de César:

bemvindosafurbnaoesquecadefazeroexercicioapresentadopeloprofessoremsaladeaula
hks|otjuygl{xhtgukyw{kigjklg�kxuk~kxioiougvxkyktzgjuvkruvxulkyyuxksygrgjkg{rg
bemvindosafurbnaoesquecadefazeroexercicioapresentadopeloprofessoremsaladeaula
---------------
Cifra de Monoalfabética:

ogulvrhxebksjorbxgeasgibhgkbcgjxgfgjivivxbzjgegrmbhxzgyxzjxkgeexjguebybhgbsyb
bemvindosafurbnaoesquecadefazeroexercicioapresentadopeloprofessoremsaladeaula