1.已知字符串:"this is a test of java".按要求执行以下操作:(要求源代码、结果截图。)
① 统计该字符串中字母s出现的次数。
② 统计该字符串中子串“is”出现的次数。
③ 统计该字符串中单词“is”出现的次数。
④ 实现该字符串的倒序输出。
package 五;
public class 题目1 {
public static void main(String[] args) {
String str="this is a test of java";
int x=(str.split("s")).length-1;
int y=(str.split("is")).length-1;
int z=(str.split(" is ")).length-1;
StringBuffer sb=new StringBuffer(str);
System.out.println("s出现的次数为"+x);
System.out.println("is出现的次数为"+y);
System.out.println("单词is出现的字数"+z);
System.out.println("字符串倒叙输出;"+sb.reverse().toString());
}
}
2.请编写一个程序,使用下述算法加密或解密用户输入的英文字串。要求源代码、结果截图。
package 五;
import java.util.Scanner;
public class 题目2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(" 加密还是解密 ?");
Scanner input=new Scanner(System.in);
int x;
if(input.nextLine().equals("解密")){
x=0;
}
else {
x=1;
}
String s=input.nextLine();
char[] ch=s.toCharArray();
if(x==1) {
for(int i=0;i<ch.length;i++){
ch[i]=(char)(ch[i]+3);
}
}else {
for(int i=0;i<ch.length;i++){
ch[i]=(char)(ch[i]-3);
}
}
s=s.valueOf(ch);
System.out.println(s);
}
}
.3.已知字符串“ddejidsEFALDFfnef2357 3ed”。输出字符串里的大写字母数,小写英文字母数,非英文字母数。
package 五;
public class 实验三 {
public static void main(String[] args) {
String str="ddejidsEFALDFfnef2357 3ed";
int d=0,x=0,f=0;
for(int i=0;i<str.length();i++) {
char c=str.charAt(i);
if(c>='a'&&c<='z') {
x++;
}
else if(c>='A'&&c<='Z') {
d++;
}
else {
f++;
}
}
System.out.println("大写字母为;"+d+"小写字母为;"+x+"非英文字母数;"+f);
}
第四周总结
1 主要学习了String类的相关知识
2详细介绍了string类的常用方法。
3本周题目二实在是不会做呀,完全没有一点头绪·
4stringBuilder与stringBuffer字符串变量。stringBuilder中的attend方法与insert方法在添加字符或字符串时有着很好的作用。