实验内容
1.已知字符串:"this is a test of java".按要求执行以下操作:(要求源代码、结果截图。)
1.统计该字符串中字母s出现的次数。
代码:
package test;
public class test {
public static void main(String [] args){
int sum = 0;
String str = "this is test of java";
char c[] = str.toCharArray();
for(int i = 0;i<c.length;i++) {
if(c[i] == 's') {
sum++;
}
}
System.out.println(sum);
}
}
知识兔2.统计该字符串中子串“is”出现的次数。
代码:
package test;
public class test {
public static void main(String [] args){
int sum = 0;
String str = "this is test of java";
char c[] = str.toCharArray();
for(int i = 0;i<c.length;i++) {
if(c[i] == 'i'&&c[i+1] == 's') {
sum++;
}
}
System.out.println(sum);
}
}
知识兔3.统计该字符串中单词“is”出现的次数。
代码:
package test;
public class test {
public static void main(String [] args){
int sum = 0;
String str = "this is test of java";
char c[] = str.toCharArray();
for(int i = 0;i<c.length;i++) {
if(c[i] == ' '&&c[i+1] == 'i') {
sum++;
}
}
System.out.println(sum);
}
}
知识兔4.实现该字符串的倒序输出。
代码:
package test;
public class ban {
public static void main(String[] args) {
String str = "this is test of java";
for (int j = str.length()-1; j>=0; j--) {
char a = str.charAt(j);
System.out.print(a);
}
}
}
知识兔