第五周学习总结&实验报告三
实验报告:
实验一已知字符串:"this is a test of java".按要求执行以下操作:
1:统计该字符串中字母s出现的次数
实验代码:
package Java11;
public class TZQD {
public static void main(String[] args) {
String r = "this is a test of java";
int sum = 0;
char[] c = r.toCharArray();
for (int i = 0; i < c.length; i++) {
if (c[i] == 's') {
sum++;
}}
System.out.println("S的个数为"+sum);
}
}
2.统计该字符串中子串“is”出现的次数。
实验代码:
package Java11;
public class TZQD {
public static void main(String[] args) {
String c = "this is a test of java";
int sun = 0;
char[] r = c.toCharArray();
for (int i = 0; i < r.length; i++) {
if (r[i] == 'i' && r[i + 1] == 's') {
sun++;
}
}
System.out.println("is的个数为"+sun);
}
}
3:统计该字符串中单词“is”出现的次数。
实验代码:
package Java11;
public class TZQD {
public static void main(String[] args) {
String r = "this is a test java";
String t[] = r.split(" ");
int sum = 0;
for (int i = 0; i < t.length; i++) {
if (t[i].equals("is")) {
sum++;
}
}
System.out.println("is的个数为"+sum);
}
}
4:实现this is the text of java 倒序输出。
实验代码:
package Java11;
public class TZQD {
public static void main(String[] args) {
String c = "this is a test of java";
int i;
char[] r = c.toCharArray();
for (i = r.length-1; i >= 0; i--) {
System.out.print(r[i]);
}
}
}
实验二:
2.请编写一个程序,使用下述算法加密或解密用户输入的英文字串。要求源代码、结果截图。
实验代码:
package Java11;
import java.util.*;
public class TZQD {
public static void main(String[] args) {
Scanner sc =new Scanner(System.in);
String str=sc.nextLine();
char c[] = str.toCharArray();
char b;
for(int i=0;i<c.length;i++) {
b=(char)(c[i]+3);
System.out.print(""+b);
}
}
}
3.已知字符串“ddejidsEFALDFfnef2357 3ed”。输出字符串里的大写字母数,小写英文字母数,非英文字母数
实验代码:
public static void main(String[] args) {
String b="ddejidsEFALDFfnef2357 3ed";
int big=0,small=0,another=0;
char[] c=b.toCharArray();
for(int i=0;i<b.length();i++) {
if('a'<=c[i]&&c[i]<='z') {
small++;
}
else if('A'<=c[i]&&c[i]<'Z') {
big++;
}
else if('1'<=c[i]&&c[i]<='9'){
another++;
}
}
System.out.println("大写字母数:"+big);
System.out.println("小写字母数:"+small);
System.out.println("非字母数:"+another);
}
}