C语言 字符串加密

发布网友 发布时间:2022-04-23 18:16

我来回答

5个回答

热心网友 时间:2023-10-14 02:29

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(void)
{
    char c[100];
    int  k;
    int  len,i,temp;

    scanf("%s",c);
    scanf("%d",&k);

    len = (int)strlen(c);
    k = k%26;

    for(i=0;i<len;i++)
    {
        if (c[i] >= 'a' && c[i] <= 'z')
        {
            if(c[i]+k > 'z')
            {
                temp = 'z'-c[i];
                temp = k - temp - 1;
                c[i]='a'+temp;
            }
            else
            {
                c[i]+=k;
            }
        }
        else if (c[i] >= 'A' && c[i] <= 'Z')
        {
            if(c[i]+k > 'Z')
            {
                temp = 'Z'-c[i];
                temp = k - temp - 1;
                c[i]='A'+temp;
            }
            else
            {
                c[i]+=k;
            }
        }
        else
        {
            /* do nothing */
        }
    }

    printf("%s\n",c);

    return 0;
}

热心网友 时间:2023-10-14 02:29

string a = "Welcome";
string b = null;
int num = a.Length;
int ascii;
char ch;
b = a.Substring(a.Length - 1, 1);
for (int i = 0; i < num - 1; i++)
{
ascii = Convert.ToInt32(Convert.ToChar(a.Substring(i, 1)));
ch = Convert.ToChar(ascii + 3);
b += ch;
}
MessageBox.Show("明文="+a +"密文="+b);

热心网友 时间:2023-10-14 02:29

有空格跳过就行了,如下

private string Encrypt(string str)
{
string result = "";
for (int i = 0; i < str.Length; i++)
{
if (str[i] == ' ')
{
result += " ";
continue;
}
if (char.IsLetter((char)(str[i] + 1)))
result += (char)(str[i] + 1);
else
result += (char)(str[i] - 25);
}
return result;
}
private string Decrypt(string str)
{
string result = "";
for (int i = 0; i < str.Length; i++)
{
if (str[i] == ' ')
{
result += " ";
continue;
}
if (char.IsLetter((char)(str[i] - 1)))
result += (char)(str[i] - 1);
else
result += (char)(str[i] + 25);
}
return result;
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("原始字符串:asd fAY IUE WZz daa da" +
"\n加密:" + Encrypt("asd fAY IUE WZz daa da") +
"\n解密:" + Decrypt(Encrypt("asd fAY IUE WZz daa da")));
}

热心网友 时间:2023-10-14 02:30

private string Encode(string Value)
{
int l = Value.Length;
string OutputString = Value.Substring(l-1,1);
char[] buff=Value.ToCharArray(0,l-1);
for (int i = 0; i < buff.Length; i++)
OutputString += ((char)(buff[i] + 3)).ToString();
return OutputString;
}

热心网友 时间:2023-10-14 02:31

StringBuilder str=new StringBuilder("hdkahdkjsahdkas",100);
for(int i=(int)'z';i>=(int)'a';i--)
{char old1=(char)i;
char new1= (char)(i+1);
str = str.Replace(old1,new1);
}
for(int i=(int)'Z';i>=(int)'A';i--)
{char old1=(char)i;
char new1=(char)(i+1);
str = str.Replace(old1,new1);
}
Console.WriteLine(str.ToString());

大概就这样了,偷懒没考虑没考虑吧A—》Z or a—》z

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com