実験報告
1.既知の文字列は、次の要件:(ソースコード、結果のスクリーンショット)を行うために必要な「これは、Javaのテストです」。
(1)文字列sが現れ中の文字の数をカウントします。
package shyzye;
public class shsan {
public static void main(String[] args) {
// TODO Auto-generated method stub
String str="this is a test of java";
int count=0;
char[] c=str.toCharArray();
for(int i=0;i<c.length;i++) {
if(c[i]=='s') {
count++;
}
}
System.out.println("S的个数:"+count);
}
}
(2)文字列「である」内の単語の数をカウント表示されます。
package shyzye;
public class shsan2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
String str="this is a test of java";
int count=0;
String[] c=str.split(" ");
for(String e:c) {
if(e.equals("is")) {
count++;
}
}
System.out.println("is作为单词出现的次数:"+count);
}
}
(3)文字列は中性子の文字列が「ある」が表示された回数をカウントします。
package shyzye;
public class shsan3 {
public static void main(String[] args) {
String str="this is a test of java";
int i=0;
int count=0;
while(str.indexOf("is",i)!=-1){
count++;
i=str.indexOf("is",i)+1;
}
System.out.println("is作为字符出现的次数:"+count);
}
}
(4)文字列が出力される逆得ます。
package shyzye;
public class shsan4 {
public static void main(String[] args) {
// TODO Auto-generated method stub
StringBuffer str=new StringBuffer();
str.append("this is a test of java");
System.out.print("倒序:"+str.reverse());
}
}
2.ユーザーによる英語の文字列の入力を暗号化または復号化するために、次のアルゴリズムを使用して、プログラムを書きます。これは、ソースコード、結果のスクリーンショットが必要です。
package shyzye;
import java.util.*;
public class shsan二 {
public static void main(String[] args) {
int i,j=0;
Scanner scanf=new Scanner(System.in);
String str=scanf.nextLine();
char chr[]=str.toCharArray();
char[] chr1=null;
chr1 = new char[str.length()];
for(i=chr.length-3;i<chr.length;i++) {
chr1[j]=chr[i];
j++;
}
for(i=0;i<chr.length-3;i++){
chr1[j]=chr[i];
j++;
}
System.out.print(chr1);
}
}
3.文字列 "ddejidsEFALDFfnef2357の3ED" を考えます。大文字の数で出力文字列、小文字は、英語以外の文字の数をカウントします。
package shyzye;
public class shsans {
public static void main(String[] args) {
// TODO Auto-generated method stub
String str1="ddejidsEFALDFfnef2357 3ed";
int count1=0,count2=0,count3=0;
char[] up=str1.toCharArray();
for(int i=0;i<up.length;i++) {
if(up[i]>='a'&&up[i]<='z') {
count1++;
}
else if(up[i]>='A'&&up[i]<='Z') {
count2++;
}
else {
count3++;
}
}
System.out.println("小写英文字母:"+count1);
System.out.println("大写英文字母:"+count2);
System.out.println("其他字符:"+count3);
}
}