java-String Practice

Source: Internet
Author: User

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

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.