How to decrypt a string from numbers to characters in C++

1 Answer

0 votes
#include <iostream>
#include <sstream>

char convert_to_char_code(std::string str) {
    std::stringstream ss(str);
    int num;
    
    ss >> num;
    
    return num + 96;
}

std::string decrypt(std::string s) {
    std::stringstream ss;
    int i = 0;
        
    while (i < s.length()) {
        char ch;
            
        if (s[i + 2] == '#') {
            ch = (char)convert_to_char_code(s.substr(i, 2) );
            i += 2;
        } else {
            ch = (char)convert_to_char_code(s.substr(i, 1));
        }
        
        i++;
        
        ss << ch;
    }

    return ss.str();
}
    
int main()
{
    std::cout << decrypt("10#1426#25#524#");
}




/*
run:

jadzyex

*/

 



answered Jul 15, 2023 by avibootz
edited Jul 15, 2023 by avibootz
...