Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <iostream>
- #include <cctype>
- #include <cstring>
- using namespace std;
- /*
- etalon
- 025843
- */
- void decodificare(char cuv_cod[], char cod[], char cuv_decod[])
- {//de la coada
- int len = strlen(cuv_cod);
- cuv_decod[0] = cuv_cod[len - 1];
- cuv_decod[1] = '\0', cuv_cod[len - 1] = '\0';
- char copie[101];//pentru strcpy-uri fara accidente
- for (int i = len - 2; i >= 0; --i)
- {
- int cif = cod[i] - '0';
- /*
- par -> luam cifra de la inceptul cuvantului codificat
- impar -> -||- finalul cuvantului codificat
- ambele le punem la inceputul cuvantului DEcodificat
- */
- char lit;
- if (!(cif % 2))
- {
- lit = cuv_cod[0];
- strcpy(cuv_cod, cuv_cod + 1);//aparent nu mi se dubleaza ultimul caracter
- int l = strlen(cuv_cod);
- cuv_cod[l] = '\0';
- }
- else
- {
- int l = strlen(cuv_cod);
- lit = cuv_cod[l - 1];
- cuv_cod[l - 1] = '\0';
- }
- strcpy(copie, cuv_decod);
- strcpy(cuv_decod + 1, copie);
- cuv_decod[0] = lit;
- int l = strlen(cuv_decod);
- cuv_decod[l] = '\0';
- }
- }
- void codificare(char cuv[], char cod[], char cuv_cod[])
- {
- cuv_cod[0] = cuv[0];
- cuv_cod[1] = '\0';//important
- char copie[101];
- for (int i = 1; cod[i]; ++i)
- {
- int cif = cod[i] - '0';
- if (!(cif % 2))
- {//la inceput
- strcpy(copie, cuv_cod);
- strcpy(cuv_cod + 1, copie);
- cuv_cod[0] = cuv[i];
- int l = strlen(cuv_cod);
- cuv_cod[l] = '\0';
- }
- else
- {
- int l = strlen(cuv_cod);
- cuv_cod[l] = cuv[i];
- cuv_cod[l + 1] = '\0';
- }
- }
- }
- bool verificare_cod(char cuv[], char cod[])
- {
- int l_cuv = strlen(cuv), l_cod=strlen(cod) ;
- if (l_cuv==l_cod)
- return true;
- else
- return false;
- }
- void citire(char cuv[], char cod[])
- {
- cin.getline(cuv, 100);
- cin.getline(cod, 100);
- }
- int main()
- {
- char cuv[101], cod[101];
- citire(cuv, cod);
- if (!verificare_cod(cuv, cod))
- {
- cout << "cod incorect";
- return 0;
- }
- char cuv_cod[101];
- codificare(cuv, cod, cuv_cod);
- cout << "\nCuvantul codificat: " << cuv_cod << '\n';
- char cuv_decod[101];
- decodificare(cuv_cod, cod, cuv_decod);
- cout <<"Cuvantul decodificat: " << cuv_decod << '\n';
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment