1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
|
//--------------------------------------
#include <windows.h>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <string>
//--------------------------------------
using namespace std;
//--------------------------------------
std::string encode( const std::string &s )
{
const char *from = " 0123456789"; // substitute for your string literal
const char *to[] = { "SPACE", "ZERO", "ONE", "TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN", "EIGHT", "NINE"}; // substitute for your string literals
std::string t = s;
for ( std::string::size_type pos = 0; pos < t.size(); /* */ )
{
pos = t.find_first_of( from, pos );
if ( pos != std::string::npos )
{
std::string::size_type i = std::strchr( from, t[pos] ) - from;
t.replace( pos, 1, to[i] );
pos += std::strlen( to[i] );
}
}
return ( t );
}
//--------------------------------------
std::string decode( const std::string &s )
{
const char *from = { "SPACE", "ZERO", "ONE", "TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN", "EIGHT", "NINE"}; // substitute for your string literals
const char *to[] = " 0123456789"; // substitute for your string literals
std::string t = s;
for ( std::string::size_type pos = 0; pos < t.size(); /* */ )
{
pos = t.find_first_of( from[], pos );
if ( pos != std::string::npos )
{
std::string::size_type i = std::strchr( from[], t[pos] ) - from;
t.replace( pos, 1, to[i] );
pos += std::strlen( to[i] );
}
}
return ( t );
}
//--------------------------------------
int main()
{
string text_encode;
text_encode = encode(" -0-1-2-3-4-5-6-7-8-9");
cout << text_encode << endl;
string text_decode;
text_decode = decode(text_encode);
cout << text_decode << endl;
cin.get();
}
|