Java实验三
String01
1 package com.itcast.atd.demo04; 2 3 public class String01 { 4 public static void main(String[] args) { 5 String str="This is a text of java"; 6 int count = 0; 7 8 for (int i = 0; i <str.length() ; i++) { 9 if(str.charAt(i)=='s') 10 count++; 11 } 12 13 System.out.println(count); 14 } 15 }
String02
1 package com.itcast.atd.demo04; 2 3 public class String02 { 4 public static void main(String[] args) { 5 String str="This is a text of java"; 6 int count=0; 7 int index=0; 8 String ctr="is"; 9 10 while((index=str.indexOf(ctr,index))!=-1){ 11 index+=ctr.length(); 12 count++; 13 } 14 15 System.out.println(count); 16 17 } 18 }
String03
package shiyans; public class Shiyan3 { public static void main(String[] args) { String str="this is a test of java"; int sum=0,i,n=0; for(i=0;i<str.length();i++) { if(str.indexOf(" is ", n)!=-1) { n=str.indexOf(" is ",n)+1; sum++; } } System.out.println("is单词出现次数为:"+sum); } }
2.请编写一个程序,使用下述算法加密或解密用户输入的英文字串
import java. util.*;
public class jiami{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("请输入要加密的英文字串");
String str = sc.nextLine();
char n;
String str1=new String();
for(int i=0;i<str.length();i++) {
n = str.charAt(i);
n = (char)(n+3); //字符后移3位
str1+=n;
}
System.out.println("加密后的子串是:\n"+str1);
}
}
3.
package com.itcast.atd.demo04; public class String04 { public static void main(String[] args) { String s="ddejidsEFALDFfnef2357 3ea"; int lcount = 0,ucount = 0,icount = 0; for(int i=0;i<s.length();i++) { char c=s.charAt(i);//返回字符串第i个字符 if(c>='a'&&c<='z') lcount++; else if(c>='A'&&c<='Z') ucount++; else icount++; } System.out.println(lcount); System.out.println(ucount); System.out.println(icount); } }
运行结果:
结论:以上题目都是由我个人所写,最主要的是熟悉了string类 的用法。