First question
String encryption issues
1. Programming Ideas
Read the string and get its length, take advantage of charat () to get each positional character and encrypt the word multibyte 3, and deposit it into the new string. If you encounter XYZ, it is reduced by 26.
2. Program Flowchart
3. Program source code
ImportJava.util.Scanner;ImportJava.util.Random; Public classStringjiami { Public Static voidMain (string[] args) {//TODO auto-generated Method StubStr s=NewStr (); Scanner input=NewScanner (system.in); System.out.print ("1 string encryption \n2 string decryption \ n Please select:");intChoose=input.nextint ();if(choose==1) {String str; System.out.print ("Please enter a string to encrypt:"); Input.nextline (); Str=input.nextline ();//Input Strings.setstr (str); System.out.println ("After encryption:" +S.jiami ());}if(choose==2) {System.out.print ("Please enter the string to decrypt:"); Input.nextline (); String Str=input.nextline ();//Input Strings.setstr (str); System.out.println ("After decryption:" +S.jiemi ()); }}}classstr{PrivateString str; Public voidSetstr (String a) { This. str=A;} PublicString Jiemi () {intL=str.length ();//Calculating string LengthsString newstr= "";//set an empty string to save the decrypted contentCharC; for(inti=0;i<l;i++) {C=str.charat (i);//mention of the I characterif(c>= ' A ' &&c<= ' C ') | | (c>= ' A ' &&c<= ' C ')) C=(Char) (c+26);//Decryption Processingif(c== ") C=C;ElseC=(Char) (c-3); Newstr+=C;}returnNEWSTR;//returns the decrypted string} PublicString Jiami () {intL=str.length ();//Calculating string LengthsString newstr= "";//set an empty string to save the decrypted contentCharC; for(inti=0;i<l;i++) {C=str.charat (i);//mention of the I characterif((c>= ' x ' &&c<= ' z ') | | (c>= ' X ' &&c<= ' Z ')) C=(Char) (C-26);//Decryption Processingif(c== ") C=C;ElseC=(Char) (c+3); Newstr+=C;}returnNEWSTR;//returns the decrypted string}}
4 Validation Results
Second question
Summarize the instructions for using some of the methods of the String class
how Equals is implemented
String anotherstring = (string) anobject;
A string is a reference data type and is itself a a String object,
This is the AnObject object that is passed in here , assigned to anotherstring (requires type conversion)
Of course he can call Members of the String class, you say count,value,offset are all String as a search agent
int n = count;// this count is the length of the original string
if (n = = Anotherstring.count) {//String length compared to the length of the string to be compared, the length is different than the string content
Char v1[] = value;// storing the original string literal as an array
Char v2[] = Anotherstring.value; to compare string literals into arrays
int i = offset;// array subscript
int j = Anotherstring.offset; //????
while (n--! = 0) {// iterate over array, compare array elements for same
if (v1[i++]! = v2[j++])
Return false;// returns False if there is a difference in the traversal process ;
}// If you have used equals This method should know that it returns a boolean value
return true;
}
}
return false;
}
(1) Length (): is a property that represents the length of a string. can be used to count the length of a string or to set the number of returned array elements.
(2) CharAt (): method returns the char value at the specified index . The index range is from 0 to length ()-1. For an array index, the first char value of a sequence is at index 0, index 1, and so on.
(3) void getChars (int srcbegin, int srcend,char[] DST, int dstbegin): This method copies the target characters into a string where,srcbegin For the start position of the copy,srcend is the end position of the copy, the string value DST is the target character array,dstbegin The starting position for the copy of the target character array
(4) Replace (): string replace (char Oldchar,char newchar);/ / replaces The first OldChar in a string with the Newchar;
(5) toUpperCase () converts characters to uppercase
(6) toLowerCase () converts a character to lowercase
(7) Trim () deletes the space at the beginning and end of the string, and then returns the result after the deletion. Do not delete spaces in the middle of a string
(8) ToCharArray () converts a String type to a character array type
Java Classroom Assignment 4