AlexAvram

Untitled

Jan 31st, 2026
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.18 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2.  
  3. #include <iostream>
  4. #include <cctype>
  5. #include <cstring>
  6.  
  7. using namespace std;   
  8.  
  9. /*
  10. etalon
  11. 025843
  12. */
  13.  
  14. void decodificare(char cuv_cod[], char cod[], char cuv_decod[])
  15. {//de la coada
  16.     int len = strlen(cuv_cod);
  17.     cuv_decod[0] = cuv_cod[len - 1];
  18.     cuv_decod[1] = '\0', cuv_cod[len - 1] = '\0';
  19.  
  20.     char copie[101];//pentru strcpy-uri fara accidente
  21.     for (int i = len - 2; i >= 0; --i)
  22.     {
  23.         int cif = cod[i] - '0';
  24.         /*
  25.         par -> luam cifra de la inceptul cuvantului codificat
  26.         impar -> -||- finalul cuvantului codificat
  27.         ambele le punem la inceputul cuvantului DEcodificat
  28.         */
  29.         char lit;
  30.         if (!(cif % 2))
  31.         {
  32.             lit = cuv_cod[0];
  33.             strcpy(cuv_cod, cuv_cod + 1);//aparent nu mi se dubleaza ultimul caracter
  34.  
  35.             int l = strlen(cuv_cod);
  36.             cuv_cod[l] = '\0';
  37.         }
  38.         else
  39.         {
  40.             int l = strlen(cuv_cod);
  41.             lit = cuv_cod[l - 1];
  42.             cuv_cod[l - 1] = '\0';
  43.         }
  44.  
  45.         strcpy(copie, cuv_decod);
  46.         strcpy(cuv_decod + 1, copie);
  47.         cuv_decod[0] = lit;
  48.  
  49.         int l = strlen(cuv_decod);
  50.         cuv_decod[l] = '\0';
  51.     }
  52. }
  53. void codificare(char cuv[], char cod[], char cuv_cod[])
  54. {
  55.     cuv_cod[0] = cuv[0];
  56.     cuv_cod[1] = '\0';//important
  57.     char copie[101];
  58.  
  59.     for (int i = 1; cod[i]; ++i)
  60.     {
  61.         int cif = cod[i] - '0';
  62.         if (!(cif % 2))
  63.         {//la inceput
  64.             strcpy(copie, cuv_cod);
  65.             strcpy(cuv_cod + 1,  copie);
  66.             cuv_cod[0] = cuv[i];
  67.            
  68.             int l = strlen(cuv_cod);
  69.             cuv_cod[l] = '\0';
  70.         }
  71.         else
  72.         {
  73.             int l = strlen(cuv_cod);
  74.             cuv_cod[l] = cuv[i];
  75.  
  76.             cuv_cod[l + 1] = '\0';
  77.         }
  78.     }
  79. }
  80.  
  81. bool verificare_cod(char cuv[], char cod[])
  82. {
  83.     int l_cuv = strlen(cuv), l_cod=strlen(cod) ;
  84.    
  85.     if (l_cuv==l_cod)
  86.         return true;
  87.     else
  88.         return false;
  89. }
  90. void citire(char cuv[], char cod[])
  91. {
  92.     cin.getline(cuv, 100);
  93.     cin.getline(cod, 100);
  94. }
  95.  
  96.  
  97. int main()
  98. {
  99.     char cuv[101], cod[101];
  100.     citire(cuv, cod);
  101.    
  102.     if (!verificare_cod(cuv, cod))
  103.     {
  104.         cout << "cod incorect";
  105.         return 0;
  106.     }
  107.  
  108.     char cuv_cod[101];
  109.     codificare(cuv, cod, cuv_cod);
  110.     cout << "\nCuvantul codificat: " << cuv_cod << '\n';
  111.    
  112.     char cuv_decod[101];
  113.     decodificare(cuv_cod, cod, cuv_decod);
  114.     cout <<"Cuvantul decodificat: " << cuv_decod << '\n';
  115.    
  116.        
  117.     return 0;
  118. }
  119.  
  120.  
Advertisement
Add Comment
Please, Sign In to add comment