Course Summary: Learn about common methods String class and learned a lot about some applications Stirng class of problems in the process of doing.
1. Known string: "this is a test of java" required to do the following requirements :( source code, the results screenshot).
- The number of letters in the string s occurs statistics.
- The string neutron count the number of times the string "is" appears.
- The number of words in the string "is" appears in the statistics.
- Achieve reverse the string is output.
package twst;
public class test {
public static void main(String args[]) {
String str="this is a test of java";
int x=(str.split("s")).length-1;
System.out.println("s出现的次数="+x);
int n=(str.split("is")).length-1;
System.out.println("is出现的次数="+n);
char s='s',i='i';
char c[]=str.toCharArray();
int count=0;
for(int j=0;j<c.length;j++) {
if(' '==c[j] && i==c[j+1] && s==c[j+2] && ' '==c[j+3]) {
count++;
}
}
System.out.println("单词is出现的次数="+count);
for(int a=c.length-1;a>=0;a--) {
System.out.print(c[a]);
}
}
}
public static void main(String args[]) {
String str="this is a test of java";
int x=(str.split("s")).length-1;
System.out.println("s出现的次数="+x);
int n=(str.split("is")).length-1;
System.out.println("is出现的次数="+n);
char s='s',i='i';
char c[]=str.toCharArray();
int count=0;
for(int j=0;j<c.length;j++) {
if(' '==c[j] && i==c[j+1] && s==c[j+2] && ' '==c[j+3]) {
count++;
}
}
System.out.println("单词is出现的次数="+count);
for(int a=c.length-1;a>=0;a--) {
System.out.print(c[a]);
}
}
}
2. Write a program, using the following algorithm to encrypt or decrypt the English string input by the user. It requires source code, the results screenshot.
package twst; public class jiami { public String jiami(String s1){ int i,j; String allstring="ABCDEFGABC"; StringBuffer ss = new StringBuffer(); for(j=0;j<s1.length();j++) { char s2=s1.charAt(j); for(i=0;i<allstring.length()-3;i++){ char allstring1=allstring.charAt(i); char allstring2=allstring.charAt(i+3); if(allstring1==s2) { ss.append(allstring2); break; } } } String s3 = ss.toString(); return s3; } } package twst; public class password { public static void main(String[] args){ jiami newpassword=new jiami(); String str="ABCDEFH"; System.out.print(newpassword.jiami(str)); } }
Question: This question can not write, I'm still a little not write.
3. Given the string "ddejidsEFALDFfnef2357 3ed". The output string in the number of capital letters, lowercase letters count, the number of non-English letters.
package twst; public class test { public static void main(String args[]) { String str="ddejidsEFALDFfnef2357 3ed"; char c[]=str.toCharArray(); for(int i=0;i<c.length;i++) { if(c[i]>='A'&&c[i]<='Z') { System.out.println("大写英文字母="+c[i]); } else if(c[i]>='a'&&c[i]<='z') { System.out.println("小写英文字母="+c[i]); } the else { System.out.println ( "other characters =" + C [I]); } } } }