First, string encryption
The ancient Roman Emperor Caesar used the following methods to encrypt military information during the war:
Please write a program, using the above algorithm to encrypt or decrypt the user input of the English string requirements design ideas, program flowchart, source code, results.
1. Program Design Idea
First define the string, then enter the string, figure out the length of the string, with a for loop to encrypt the string, using the ASCLL Code table, to encrypt, if it is XYZ 23,xyz the same, the rest of the normal plus 3, and finally the encrypted word multibyte to an empty string type of member, The result is the final output.
2. Program Flowchart
3. Source code
ImportJava.util.Scanner; Public classTest { Public Static voidMain (string[] args) {Scanner input=NewScanner (system.in); System.out.println ("Please enter a string to encrypt:"); String Str=""; String str1=""; intN=0; STR=Input.nextline (); N=str.length (); Chartemp = 0; for(inti=0;i<n;i++) { if((Str.charat (i) > && str.charat (i) < 88) | | (Str.charat (i) > && str.charat (i) < 120)) Temp=(Char) (Str.charat (i) + 3); Else if((Str.charat (i) > && str.charat (i) < 91) | | (Str.charat (i) > 119 && Str.charat (i) < 123)) Temp=(Char) (Str.charat (i)-23); STR1+=temp; } System.out.println ("The encrypted string is: \ n" +str1); }}
4. Results
Ii. description of various functions of string
1, String.Equals (): Used to detect two string types of objects are equal, can not simply use "= =" to determine the equality of two strings.
2, Length (): You can find the length of a string.
3, CharAt (): Used to return the char value at the specified index.
4, GetChars (): Copies the character from this string to the target character array.
5. Replace (): Used to replace some characters with some character in a string.
6, toUpperCase (): Converts the string to uppercase.
7, toLowerCase (): Convert the string to lowercase.
8. Trim (): Gets the value of the field of the object, then turns into a string type, and removes the front and back blanks.
9, ToCharArray (): Converts a string content to a character array.
Java string encryption and description of various functions of string