题目描述:编写程序,输出字符串中的大写字母、小写小母和其他的个数。如有一个字符串"Helle, This is A test textfile.123456, tannk you!!",则其大写字母个数:3,小写字母个数:29,其他字符个数:18.
这里提供了四种算法,第一种是我们比较好理解的,也属于硬编码问题,其他三种方法要借助JAVA语言的jdk提供的api。
方法一:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>js分析字符串内容</title>
<!--实现一个函数,输出某字符串里有几个大写字母,小写字母,数字,其他符号。字符串由形参指定 -->
<script>
var str = prompt("请随意输入大写字母小写字母数字及符号等");
function analyze(aa){
var a = 0;
var A = 0;
var n = 0;
var other = 0;
for (var i=0;i<aa.length;i++){
var c = aa.substr(i,1); //aa.charAt(i);
if (c>='a' && c<='z'){
a++;
}else if(c>='A' && c<='Z'){
A++;
}else if(c>='0' && c<='9'){
n++;
}else{
other++;
}
}
document.write("小写字母为"+a,"大写字母为"+A,"数字为"+n,"其他字符为"+other);
}
</script>
</head>
<body onload="analyze(str)">
</body>
</html>
//方法一:在利用每个字符的Unicode码在a~z之间,调用jdk提
//供的String类的charAt取出字符串每一个字符,逐个进行比较来判定
class FindLetter {
public static void main(String[] args) {
String str = "Helle, This is A test textfile.123456, tannk you!!";
int upCount = 0;
int lowCount = 0;
int otherCount = 0;
for(int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if(c >= 'a' && c <= 'z') {
lowCount++;
} else if(c >= 'A' && c <= 'Z') {
upCount++;
} else {
otherCount++;
}
}
System.out.println("大写之母个数:" + upCount);
System.out.println("小写字母个数:" + lowCount);
System.out.println("其他字符个数:" + otherCount);
}
}
方法二:
//方法二:用jdk的Character类的isUpperCase方法和isLowerCase方法
class FindLetter1 {
public static void main(String[] args) {
String str = "Helle, This is A test textfile.123456, tannk you!!";
int upCount = 0;
int lowCount = 0;
int otherCount = 0;
for(int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if(Character.isUpperCase(c)) {
upCount++;
} else if(Character.isLowerCase(c)) {
lowCount++;
} else {
otherCount++;
}
}
System.out.println("大写字母个数:" + upCount);
System.out.println("小写字母个数:" + lowCount);
System.out.println("其他字母个数:" + otherCount);
}
}
方法三:
//方法三:先定义两个字符串a到z和A到Z,再逐个取出str字符串中的每个字母,
//用indexOf()方法来判断字符是否在这这个定义的字符串中,在大写字母这一行,
//大写字母的计数器就加1,在小写字母这行,小写字母就加一,否则其他字母计算器
//加1
class FindLetter2 {
public static void main(String[] args) {
String low = "abcdefghijklmnopqrstuvwxyz";
String up = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int lowCount = 0;
int upCount = 0;
int otherCount = 0;
String str = "Helle, This is A test textfile.123456, tannk you!!";
for(int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if(low.indexOf(c) != -1) {
lowCount++;
} else if(up.indexOf(c) != -1) {
upCount++;
} else {
otherCount++;
}
}
System.out.println("大写字母个数:" + upCount);
System.out.println("小写字母个数:" + lowCount);
System.out.println("其他字母个数:" + otherCount);
}
}
方法四:
//把str分别转化为大写和小写 大写用sU 小写 sL
//然后通过与原串比较来统计个数
class FindLetter3 {
public static void main(String[] args) {
String str = "Helle, This is A test textfile.123456, tannk you!!";
String sU = str.toUpperCase();
String sL = str.toLowerCase();
int lowCount = 0;
int upCount = 0;
int otherCount = 0;
for(int i = 0; i < str.length(); i++) {
char charSTR = str.charAt(i);
char charSU = sU.charAt(i);
char charSL = sL.charAt(i);
//如果不是字母,是其他字符,则直接用otherCount来计数
if(Character.isLetter(charSTR)) {
//如果原串与转换过后的大写字母串相等,则原来字符为大写字母,
//若与小写字母相等,则为小写字母
if( charSTR == charSU) {
upCount++;
} else if(charSTR == charSL) {
lowCount++;
}
} else {
otherCount++;
}
}
System.out.println("大写字母个数:" + upCount);
System.out.println("小写字母个数:" + lowCount);
System.out.println("其他字母个数:" + otherCount);
}
}
这四种算法都有正确的输出:
大写字母个数:3
小写字母个数:29
其他字母个数:18