# Summary of exercises of functions (Continued)

## Last time we said some simple questions, today we will look at some questions about strings.

### First question Analysis:

2. Pass the password into boolean isValid(password) and the return value is true, which is legal; the return value is false, which is illegal
3. Define isValid() method
• Length isLengthValid() the length of the string is greater than 8
• Since isContentValid() must contain letters and numbers, it uses for loop to traverse. First, it obtains the characters of the current string, and then it needs to define two methods, isLetter(c) and isDigit(c), to determine whether the character is a letter or a number. To determine whether it is a character or a number, just compare the ASCII value. You can write c > ='a '& & C < ='z' | C > ='a '& & C < ='z'; C > ='0 '& & C < ='9';
• At least two numbers isNumberValid()

All three conditions are met. If it is a "and" relationship, then it is a legal password!

```import java.util.Scanner;
class Demo04_04{
public static void main(String[] args){
Scanner scanner=new Scanner(System.in);
System.out.println("Cryptographic legality!");
}else{
}
}
public static boolean isValid(String s){
return isLengthValid(s)&&isContentValid(s)&&isNumberValid(s);
}
public static boolean isLengthValid(String s){
return s.length()>=8;
}
public static boolean isContentValid(String s){
char c=' ';
for(int i=0;i<s.length();i++){
c=s.charAt(i);
if(!isLetter(c)&&!isDigit(c)){
return false;
}
}
return true;
}
public static boolean isNumberValid(String s){
int count=0;
char c=' ';
for(int i=0;i<s.length();i++){
c=s.charAt(i);
if(isDigit(c)){
count++;
}
}
return count>=2;
}
public static boolean isLetter(char c){
return c>='a'&&c<='z' || c>='A'&&c<='Z';
}
public static boolean isDigit(char c){
return c>='0'&&c<='9';
}

}```

Results: ### Second questions Analysis: with for loop traversal, if the ith character in the string is equal to the "e" you are looking for, then count + +. Finally, count is returned.

```class Demo04_08{
public static void main(String[] args){
System.out.println(count(s,'a'));
}
public static int count(String s,char c){
int count=0;
for(int i=0;i<s.length();i++){
if(s.charAt(i)==c){
count++;
}
}
return count;
}
}```

Results: ### Third questions Analysis: we can get it by subscript, then we can directly traverse backwards, use for loop, start from (length-1), take it out respectively, and add it with res. Finally return to res

```class Demo04_09{
public static void main(String[] args){
String s="ABCD";
System.out.println(reverse(s));
}
public static String reverse(String s){
String res="";
for(int i=s.length()-1;i>=0;i--){
res+=s.charAt(i);
}
return res;
}
}```

Results: ### Fourth questions Analysis: use for loop to traverse, take out the current character, and judge whether it is a capital letter by calling the method of capital letters, which is mentioned in the first topic.

```class Demo04_10{
public static void main(String[] args){
String s="Welcome to Java";
System.out.println(getUpperCase(s));
}
public static int getUpperCase(String s){
int count=0;
for(int i=0;i<s.length();i++){
if(isUpperCase(s.charAt(i))){
count++;
}
}
return count;
}
public static boolean isUpperCase(char c){
return c>='A'&&c<='Z';
}
}```

Results: ### Fifth questions Analysis: two strings are passed in, traversed, and compared from left to right. If they are not equal, it means that all the previous strings are equal.

When the two strings are not equal, traverse the small strings, and we need disk to judge the size of the two strings. Define two pointers, starting from 0, considering three situations, s1=s2; S1 > S2; S1 < S2.

```class Demo04_11{
public static void main(String[] args){
String s1="Welcome to C++";
String s2="Welcome to programming";
System.out.println(getCommonPrefix(s1,s2));
}
public static String getCommonPrefix(String s1,String s2){
int pa=0;// Pointer to s1
int pb=0;// Pointer to s2
while(true){
if(s1.charAt(pa)!=s2.charAt(pb)){
return s1.substring(0,pa);
}
if(s1.charAt(pa)==s2.charAt(pb)){
pa++;
pb++;
}
if(pa==s1.length()){
return s1;
}
if(pb==s2.length()){
return s2;
}
}
}
}```

Results: ### Sixth questions Analysis: we should note that the hexadecimal letters are only "A-F". We take out the characters and convert ABCDEF into corresponding numbers

The method is that (C -'a '+ 10) can be directly converted, and (C -'0') for numbers

```class Demo04_12{
public static void main(String[] args){
String s="AB8C";
System.out.println(hexToDeimal(s));
}
public static int hexToDeimal(String s){
int num=0;
char c=' ';
for(int i=0;i<s.length();i++){
c=s.charAt(s.length()-1-i);
if(isLetter(c)){
num+=(c-'A'+10)*Math.pow(16,i);
}else{
num+=(c-'0')*Math.pow(16,i);
}
}
return num;
}
public static boolean isLetter(char c){
return c>='A'&&c<='F';
}
}```

Results:   Published 14 original articles, won praise 0, visited 313

Keywords: Java ascii Programming

Added by c_shelswell on Tue, 18 Feb 2020 11:40:57 +0200