Each letter in string is replaced with another letter. - C++ STL

C++ examples for STL:string

Description

Each letter in string is replaced with another letter.

Demo Code

#include <algorithm>
#include <ctime>
#include <iostream>
#include <map>
#include <string>

void initialize(std::map<char, char>&);
void encrypt(std::string&, std::map<char, char>&);
void print(const std::map<char, char>&);

int main(int argc, const char* argv[]) {
    srand((int)time(0));

    std::map<char, char> cypher;

    initialize(cypher);//from w  w w .j  av  a 2  s  .  c o  m

    std::string userStr;
    std::cout << "Enter a string to be cyphered: ";
    std::getline(std::cin, userStr);

    encrypt(userStr, cypher);

    std::cout << userStr << std::endl;

    // print(cypher);

    return 0;
}
// create the cryptogram cypher randomises alphabet and maps to original letters
void initialize(std::map<char, char>& cypher) {
    std::string alphabet = "abcdefghijklmnopqrstuvwxyz";
    std::string shuffled = alphabet;

    std::random_shuffle(shuffled.begin(), shuffled.end());

    std::string::iterator ait = alphabet.begin();
    std::string::iterator sit = shuffled.begin();

    while (ait != alphabet.end()) {
        cypher.insert(std::pair<char, char>(*(ait++), *(sit++)));
    }
}
// cypherise the given string
void encrypt(std::string& userStr, std::map<char, char>& cypher) {
    std::string::iterator it = userStr.begin();

    while (it != userStr.end()) {
        if (*it != ' ') {
            if (isupper(*it))
                *it = tolower(*it);

            *it = cypher.find(*(it++))->second;
        } else {
            ++it;
        }
    }
}
// print map contents
void print(const std::map<char, char>& cypher) {
    for (auto elem : cypher) {
        std::cout << elem.first << " " << elem.second << std::endl;
    }
}

Result


Related Tutorials