Repciu

Untitled

Jan 13th, 2024
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.02 KB | None | 0 0
  1.  
  2. #include <iostream>
  3. #include <string>
  4. using namespace std;
  5. string zakoduj_wiadomosc(string wiadomosc)
  6. {
  7. string zakodowana = "";
  8. string alfabet = "abcdefghijklmnopqrstuvwxyz";
  9. for(int i = 0; i < wiadomosc.length(); i++)
  10. {
  11. if (wiadomosc[i] >= 'a' && wiadomosc[i] <= 'z')
  12. {
  13. int index = wiadomosc[i] - 'a';
  14.  
  15. if(index == 0)
  16. {
  17. index = 25;
  18. }
  19. else
  20. {
  21. index = index - 1;
  22. }
  23. zakodowana += alfabet[index];
  24. }
  25. else
  26. {
  27. zakodowana += wiadomosc[i];
  28. }
  29. }
  30. return zakodowana;
  31. }
  32. string odkoduj_wiadomosc(string wiadomosc)
  33. {
  34. string odkodowana = "";
  35. string alfabet = "abcdefghijklmnopqrstuvwxyz";
  36. for(int i = 0; i < wiadomosc.length(); i++)
  37. {
  38. if (wiadomosc[i] >= 'a' && wiadomosc[i] <= 'z')
  39. {
  40. int index = wiadomosc[i] - 'a';
  41.  
  42. if(index == 25)
  43. {
  44. index = 0;
  45. }
  46. else
  47. {
  48. index = index + 1;
  49. }
  50. odkodowana += alfabet[index];
  51. }
  52. else
  53. {
  54. odkodowana += wiadomosc[i];
  55. }
  56. }
  57. return odkodowana;
  58. }
  59. int main() {
  60.  
  61. cout <<"Witaj w programie Lamacz kodow !" << endl;
  62. cout << "Wybierz opecacje:"<<endl;
  63. cout << "1. Zakoduj wiadomosc." << endl;
  64. cout <<"2. Odkoduj wiadomosc."<<endl;
  65. int wybor = 0;
  66. cin >> wybor;
  67. cin.ignore();
  68. string wiadomosc = "";
  69. cout << "Wprowadz wiadomosc: ";
  70. getline(cin, wiadomosc);
  71. if(wybor == 1)
  72. {
  73. string zakodowana = zakoduj_wiadomosc(wiadomosc);
  74. cout << zakodowana << endl;
  75. }
  76. else if(wybor == 2)
  77. {
  78. string odkodowana = odkoduj_wiadomosc(wiadomosc);
  79. cout << odkodowana << endl;
  80. }
  81. else
  82. {
  83. cout << "Nieprawidlowa wartosc :("<<endl;
  84. }
  85. return 0;
  86. }
Advertisement
Add Comment
Please, Sign In to add comment