古罗马凯撒大帝字串加密

1.字串加密

设计思想:用户输入字符串,选择加密or解密,调用toCharArray()将字符串放入数组中,分别调用加密或解密函数输出你要的字符串!

流程图:

 

 

 

源代码:

package 课堂4;

import java.util.Scanner;

public class Stringpassword {
public static char jiami(char c)
{
if(c=='X')
return 'A';
if(c=='Y')
return 'B';
if(c=='Z')
return 'C';
if(c=='x')
return 'a';
if(c=='y')
return 'b';
if(c=='z')
return 'c';

return (char)(c+3);
}
public static char jiemi(char c)
{
if(c=='A')
return 'X';
if(c=='B')
return 'Y';
if(c=='C')
return 'Z';
if(c=='a')
return 'x';
if(c=='b')
return 'y';
if(c=='c')
return 'z';

return (char)(c-3);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
String s="";
char c;
System.out.println("请选择加密还是解密");
Scanner in=new Scanner (System.in);
String s1=in.next();
if(s1.equals("加密"))
{
System.out.println("请输入字符串");
String s2=in.next();
char a[]=s2.toCharArray();
for(int i=0;i<s2.length();i++)
{
c=jiami(a[i]);
s+=c;
}
System.out.println("加密后:"+s);
}
else
{
System.out.println("请输入字符串");
String s2=in.next();
char a[]=s2.toCharArray();
for(int i=0;i<s2.length();i++)
{
c=jiemi(a[i]);
s+=c;
}
System.out.println("解密后:"+s);
}

}

}

结果截图:

 

 2.String.equals()方法的实现代码

 

public boolean equals(Object anObject) {

    if (this == anObject) {

        return true;

    }

    if (anObject instanceof String1) {

        String1 anotherString = (String1)anObject;

      

int n = value.length;

        if (n == anotherString.value.length) {

            char v1[] = value;

            char v2[] = anotherString.value;

            int i = 0;

            while (n-- != 0) {

                if (v1[i] != v2[i])

                    return false;

                i++;

            }

            return true;

        }

    }

    return false;

}

}

 3.

    1 string.length()是用来求字符串的长度,返回值为字符串的长度。

 2 string.charAt()为取该字符串某个位置的字符,从0开始,例如string.charAt(0)就会返回该字符串的第一个字符。

 3 string.getChars()为将这个字符串中的字符复制到目标字符数组。

 4 string.replace()为将原string 中的元素或子串替换。返回替换后的string。

 5 string.toUpperCase()为将字符串string中字符变为大写。

 6 string.toLowerCase()为将字符串string中字符变为小写。

 7 string.trim()为去除字符串的头为空格。

 8 string.toCharArray()为将字符串转换为字符数组。

源代码:

package 课堂4;

public class Exercise

{
public static void main(String args[])
{
String s1=new String("I am a student");
String s2=new String("HOW ARE YOU");
String s3=new String(" Hello ");

//string.length()求长度
System.out.println("length of string is:"+s1.length());

//string.charAt()取指定位置字符
System.out.println("the first char of string::"+s1.charAt(0));

//string.getChars()为将这个字符串中的字符复制到目标字符数组。
char[] c = new char[s1.length()];
s1.getChars(0, s1.length(), c, 0);
System.out.print("输出数组:");
for(int i=0;i<s1.length()-1; i++)
{
System.out.print(" "+c[i]);
}

//string.replace()为将原string 中的元素或子串替换。返回替换后的string。
System.out.println("\ns1替换为s2后是:"+s1.replace(s1, s2));

//string.toUpperCase()为将字符串string中字符变为大写。
System.out.println("s1变为大写后为:"+s1.toUpperCase());

//string.toLowerCase()为将字符串string中字符变为小写。
System.out.println("s2变为小写后为:"+s2.toLowerCase());

//string.trim()为去除字符串的头为空格。
System.out.println("s1变为大写后为:"+s3.trim());

//string.toCharArray()为将字符串转换为字符数组
char a[]=s1.toCharArray();
System.out.println("转换为数组a后a[0]= "+a[0]);

}


}

 

 

转载于:https://www.cnblogs.com/xiaohaigege666/p/7737984.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
c语言编写,欢迎扔板砖 //移位算 #include #include #define SIZE 50 int main() { //i 用于计数输入个数,j 为临时变量, plain 存放明文, cipher 存放密文,decryption存放解密后文本,fpp 为明文文件指针,fpc 为密文文件指针 int i,j; char plain[SIZE],cipher[SIZE],decryption[SIZE],ciphertext[SIZE]; FILE * fpp,* fpc,* fpd; //加密 //建立新的明文TXT文件 printf("Caesar algorithm\n"); if((fpp=fopen("plain.txt","w+"))==NULL) { printf("creat new plain file error!\n"); exit(0); } //输入明文 printf("input plain alphabet:\n"); i=0; scanf("%c",&plain[i]); while(plain[i]!='\n'&&i<SIZE) { i++; scanf("%c",&plain[i]); } printf("success input %d characters\n",i); //将明文转存到文件中 for(j=0;j<i;j++) { if(fwrite(&plain[j],sizeof(char),1,fpp)!=1) { printf("saving plain file error!\n"); exit(0); } } printf("success saving plain text!\n"); //加密 for(j=0;j<i;j++) { cipher[j]=plain[j]+3; if(cipher[j]99) { printf("cipher %d = %c\n",j,cipher[j]); } else if(cipher[j]>122) { cipher[j]=cipher[j]%122+96; printf("cipher %d = %c\n",j,cipher[j]); } else if(cipher[j]>90) { cipher[j]=cipher[j]%90+64; printf("cipher %d = %c\n",j,cipher[j]); } else { printf("cipher %d = %c\n",j,cipher[j]); } } //建立密文文件 if((fpc=fopen("cipher.txt","w+"))==NULL) { printf("create new cipher file error!"); exit(0); } for(j=0;j<i;j++) { if(fwrite(&cipher[j],sizeof(char),1,fpc)!=1) { printf("saving cipher file error!"); exit(0); } } printf("success saving cipher file!"); printf("\n"); //解密 printf("input ciphertext alphabet:\n"); i=0; scanf("%c",&ciphertext[i]); while(ciphertext[i]!='\n'&&i<SIZE) { i++; scanf("%c",&ciphertext[i]); } for(j=0;j<i;j++) { decryption[j]=ciphertext[j]-3; if(decryption[j]90&&decryption[j]<97) { decryption[j]=123-(97-decryption[j]); printf("character %d = %c\n",j,decryption[j]); } else {

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值