题目描述:编写程序,输出字符串中的大写字母、小写小母和其他的个数。如有一个字符串"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);
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>
-
- 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);
- }
- }
方法二:
-
- 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);
- }
- }
方法三:
-
- 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);
- }
- }
方法四:
-
- 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);
-
-
- 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
在一个字符串中,统计大写字母个数,小写字母个数,其他字符个数的四种算法
原文:https://www.cnblogs.com/workey/p/9193566.html