c++ - 土耳其字母表的凯撒密码解密

标签 c++ character-encoding

我在互联网上找到了很多例子,但找不到土耳其字母表的 Ceaser 密码解密。大多数字母与英文字母相似,但也有一些差异 这是土耳其语字母表:

A B C Ç D E F G Ğ H I Ğ K L M N O Ö P R S Ş T U Ü V Y Z

a b c ç d e f g ğ hi ı j k l m no ö p r s ş t u ü v y z

我找到了这个英文字母的代码,它没有一些字母,例如 й,Ö,Ü,Ş,ç,ğ,ı,ö,ş,ü:

#include <iostream>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
using namespace std;

int main()
{
    char code[501];
    int shift, len, i=0, j=0;

    cout << "Caesar Cipher Decoder " << endl;
    cout << "\nThis program will decrypt the entered text using Caesar Cipher." << endl;
    cout << "\nPress any key to continue...";
    _getch();

    system("cls");
    cout << "Enter the text that has to be decrypted (max 500 characters):" << endl;
    cin.getline(code, 501);
    len = strlen(code);

    while (j < len)
    {
        if(code[j] == 32)
        {
            code[j] = code[j];
        }

        j++;
    }

    po:
    cout << "\nEnter the amount of Caseser Shift in numbers: ";
    cin >> shift;
    if ((shift > 26) || (shift < 0))
    {
        cout << "\nShift value should be less than or equal to 26. Type again." << endl;
        goto po;
    }

    while (i < len)
    {

        code[i] = tolower(code[i]);
        code[i] = code[i] - shift;

        if (code[i] + shift == 32)
        {
            code[i] = code[i] + shift;
        }

        else if(
                ((code[i] + shift > 31) && (code[i] + shift < 65)
                || ((code[i] + shift > 90) && (code[i] + shift < 97))
                || ((code[i] + shift > 122) && (code[i] + shift < 128)))
                )
                {
                    code[i] = code[i] + shift;
                }

        else if (code[i] < 97)
        {
            if (code[i] == 32 - shift)
            {
                code[i] = code[i] + shift;
            }
            else
            {
                code[i] = (code[i] + 26);
            }
        }
        i++;
    }
    system("cls");
    cout << "\nYour deciphered code is: \"" << code << "\"" << endl;

    cout << "\nYour text has been decrypted." << endl;
    cout << "\nPress any key to end." << endl;
    _getch();

    return 0;
}

请帮助我使这个适用于土耳其字母表。

最佳答案

您发布的示例代码依赖于以下事实:标准拉丁字母是 ASCII 表中的连续 block 。土耳其语字母表的情况并非如此,因此您必须以不同的方式处理该问题。

我建议您使用替换表。创建一个 256 个字符的数组(字符编码表中的每个代码点对应一个),并用应该使用的字母填充每个代码点。然后迭代输入文本并通过在该数组中查找来替换每个字符。

关于c++ - 土耳其字母表的凯撒密码解密,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15548742/

相关文章:

c++ - 读取直方图后未处理的异常(使用 calcHist 创建)

C++ 强制 new[] 不分配 4 个额外的字节

c++ - 如何使用 UTF-8 编码字符/字符串

mysql存储函数返回值charset

linux - Linux Web 服务器字符编码不匹配的下载链接

haskell - 在 Latin1 编码的 Data.ByteString 和 Data.Text 之间转换

c++ - QNetworkAccessManager 错误代码 99;仅在某些系统上……

c++ - Qt-如何实现一个可读写的QSqlQueryModel?

c++ - 为什么调用 printf ("%p",这个);删除后;非法的?

java - java中的UTF-8编码,从网站检索数据