2012-06-17 82 views
1

使用C++,我试图编写一个加密字符串的程序。我必须将字母表中的每个字母映射到一个数字值:例如,a = 0,b = 1,c = 2等等。到目前为止,我已经创建了一个void函数,它将一个字符串作为参数并使用switch语句输出值。问题是,这些值是字符,而不是整数,我不能使用数学运算符来修改它们。 我的源代码低于:将数字映射到字符

#include <iostream> 
#include <cstdlib> 
#include <ctime> 
#include <cstring> 
#include <string> 
#include <cctype> 

using namespace std; 

void mapped_string(string in) 
{ string mapped_string; 
    int length = in.length(); 

    for (int i = 0; i < length; i++) 
    { 
     switch (in[i]) 
     { 
     case 'a': 
      cout<<"0"; 
      break; 
     case 'b': 
      cout<<"1"; 
      break; 
     case 'c': 
      cout<<"2"; 
      break; 
     case 'd': 
      cout<<"3"; 
      break; 
     case 'e': 
      cout<<"4"; 
      break; 
     case 'f': 
      cout<<"5"; 
      break; 
     case 'g': 
      cout<<"6"; 
      break; 
     case 'h': 
      cout<<"7"; 
      break; 
     case 'i': 
      cout<<"8"; 
      break; 
     case 'j': 
      cout<<"9"; 
      break; 
     case 'k': 
      cout<<"10"; 
      break; 
     case 'l': 
      cout<<"11"; 
      break; 
     case 'm': 
      cout<<"12"; 
      break; 
     case 'n': 
      cout<<"13"; 
      break; 
     case 'o': 
      cout<<"14"; 
      break; 
     case 'p': 
      cout<<"15"; 
      break; 
     case 'q': 
      cout<<"16"; 
      break; 
     case 'r': 
      cout<<"17"; 
      break; 
     case 's': 
      cout<<"18"; 
      break; 
     case 't': 
      cout<<"19"; 
      break; 

     default: 
      cout<< in[i]; 
     } 
    } 
} 

int main() 
{ string str1 = "Hello"; 

    mapped_string(str1); 



     cout << "Press any key to exit." << endl; 
     cin.ignore(2); 
     return 0; 
} 

我需要这个功能映射字符串到int值中的每个字符和整型值存储在一个名为mapped_string新的字符串。

回答

0
//stringstream is used as string 'builder'. 
std::stringstream storedstr; 

for (int i = 0; i < length; i++) 
{ 
    //As op's code but without the lengthy switch statement. Just cast to get an int. 
    int intEquiv = (int)(in[i]); 
    if (in[i] > 'z' || in[i] < 'a') intEquiv = (int)(in[i]) - 'a' 
    storedstr << intEquiv; 

    //Show what we are adding, like in original loop; 
    std::stringstream ss; 
    ss << intEquiv; 
    std::cout << ss.str(); 
} 
mapped_string = storedstr.str(); 
0
字符数

在C/C++有数值,ASCII格式, 'A' 是65, 'B' 是66 ... 'a' 为97, 'b' 为98等,作为这样你可以应用它们的数学运算,它可能更容易简单地粘到众所周知的ASCII格式,而不是创建自己的,http://en.wikipedia.org/wiki/ASCII

string str1 = "Hello"; 
for (int index = 0; index < str1.length(); index++) 
    cout << "char: " << str1[index] << " ascii numeric values: " << (int)(str1[index]) << endl; 

打印:

char: H ascii numeric values: 72 
char: e ascii numeric values: 101 
char: l ascii numeric values: 108 
char: l ascii numeric values: 108 
char: o ascii numeric values: 111 

简单/幼稚加密计划将是简单地转换字符b y一定的数量(移位密码),但在使用加密方案时,根据您正在实施的方案,您应该始终注意溢出。

string str1 = "Hello", cipher = "";  
    for (int index = 0; index < str1.length(); index++) 
     cipher += (char)(str1[index] + 10); 
    cout << "original: " << str1 << " cipher " << cipher << endl; 

打印:

original: Hello cipher Rovvy 

找回原来只是减去偏移。

0

用C++ 11:

#include <iostream> 
#include <string> 
#include <algorithm> 
using namespace std; 

string mapped_string(string in, char password) 
{ 
    transform(in.begin(), in.end(), in.begin(), [password](char& x){ return x^password; }); 
    return in; 
} 

int main(int argc, char* argv[]) 
{ 
    char password = '1'; 
    string str1 = "Hello"; 
    string str2 = mapped_string(str1, password); // encrypt str1 
    string str3 = mapped_string(str2, password); // decrypt str2 
    cout << "Original: " << str1 << endl; 
    cout << "Encrypt: " << str2 << endl; 
    cout << "Decrypt: " << str3 << endl; 
    cin.ignore(); 
    return 0; 
} 

输出:
原件:你好
加密:YT]]
解密:你好