Java string encryption and description of various functions of string

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.