Java练习:编写程序统计字符串“The String class represents character strings”中字母“s”的出现次数,区分大小写。
package com.stx.p2;
public class MoYi {
public static void main(String[] args) {
String str = "The String class represents character strings";
//将字符串转为数组
char[] array = str.toCharArray();
int a = 0;
int b = 0;
for (int i = 0; i < array.length; i++) {
if (array[i] == 's'){
a = a+1;
}
if (array[i] == 'S'){
b = b+1;
}
}
System.out.println("小写的s出现的次数是:"+a);
System.out.println("大写的S出现的次数是:"+b);
}
}