1. Implement StartsWith and EndsWith functions with your own algorithm.
String str= "DSJHAJDL";
Scanner sc=new Scanner (system.in);
SYSTEM.OUT.PRINTLN ("Input English characters:");
String St=sc.nextline ();
int S=str.indexof (ST);
if (s==0)
{
SYSTEM.OUT.PRINTLN ("Input is the first character");
}
Else
{
SYSTEM.OUT.PRINTLN ("Input is not the first character");
}
2. Character text encryption and decryption are implemented by the method of shifting characters.
Scanner sc=new Scanner (system.in);
System.out.println ("Please enter an encrypted string");
String St=sc.nextline ();
System.out.println (St.replaceall ("A", "B"). ReplaceAll ("B", "C"). ReplaceAll ("C", "D"). ReplaceAll ("D", "E"). ReplaceAll ("E", "F"). ReplaceAll ("F", "G"). ReplaceAll ("G", "H"). ReplaceAll ("H", "I"). ReplaceAll ("I", "J"). ReplaceAll ("J", "K"). ReplaceAll ("K", "L"). ReplaceAll ("L", "M"). ReplaceAll ("M", "N"). ReplaceAll ("n", "O"). ReplaceAll ("O", "P"). ReplaceAll ("P", "Q"). ReplaceAll ("Q", "R"). ReplaceAll ("R", "s"). ReplaceAll ("s", "T"). ReplaceAll ("T", "U"). ReplaceAll ("U", "V"). ReplaceAll ("V", "w"). ReplaceAll ("W", "X"). ReplaceAll ("x", "Y"). ReplaceAll ("Y", "Z"). ReplaceAll ("Z", "A")) ;
3. Randomly generate 4-bit verification code, the user input and verify that the input is correct, if the input error to generate a new verification code to allow users to re-enter, up to 5 times
String str= "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
Char[]arr=new char[4];//defines an array of type char with a length of 4
Random sj=new random ();
System.out.println ("Verification Code is:");
for (int i=0;i<4;i++)
{
Arr[i]=str.charat (Sj.nextint (61));//randomly intercept 4 individual characters from STR and assign values to the ARR array for storage
}
System.out.println (arr);
Scanner sc=new Scanner (system.in);
System.out.println ("Please enter the verification Code");
String A=new string (arr);//convert array to string
Define the number of inputs
for (int j=0;j<5;j++)
{
if (Sc.nextline (). Equals (a))
{
System.out.println ("Verification code input is correct");
}
Else
{
System.out.println ("Verification code input is wrong, please re-enter");
if (j<=3)
{
System.out.print ("Please enter the verification Code");
for (int i=0;i<4;i++)
{
Arr[i]=str.charat (Sj.nextint (61));//randomly intercept 4 individual characters from STR and assign values to the ARR array for storage
}
System.out.println (arr);
A=new String (arr);
}
Else
{
SYSTEM.OUT.PRINTLN ("Wrong input, sorry, 5 opportunities have been exhausted");
}
}
}
Java-String Practice