统计字符串中大写字母个数

可将字符串转为字符数组,然后对数组进行遍历,进而统计大写字母的个数。

下面给出代码:

import java.util.Scanner;

public class Main {
  public static void main(String args[]){
    Scanner in = new Scanner(System.in);
    String str = in.nextLine();
    int count = 0;
    char ch[] = str.toCharArray();  //转为字符数组
    for(int i = 0;i<ch.length;i++){
      if(ch[i]>=‘A‘&&ch[i]<=‘Z‘){
      count++;  //遍历数组,进行统计
    }
  }
    System.out.println(count);
  }
}

时间: 2024-08-05 08:06:35

统计字符串中大写字母个数的相关文章

c语言求字符串中大写字母个数,单词个数,子串个数及一个整数是否为回文数

#include <stdio.h> #include <ctype.h> #pragma mark 统计从终端输入的字符中每个大写字母的个数.用#号作为输入结束标志 int main() { int num[26] = {0}, i; char c; while ((c = getchar())!='#') { if (isupper(c)) { num[c-65]++; } } for (int i = 0; i<26; i++) { if (num[i]) { prin

统计一个字符串中大写字母字符,小写字母字符,数字字符出现的次数,其他字符出现的次数。

/** * A:案例演示 * 需求:统计一个字符串中大写字母字符,小写字母字符,数字字符出现的次数,其他字符出现的次数. * [email protected]#$%^ * 分析:字符串是有字符组成的,而字符的值都是有范围的,通过范围来判断是否包含该字符 * 如果包含就让计数器变量自增 */ public static void main(String[] args) { String s = "[email protected]#$%^"; int big = 0; int smal

统计字符串中汉字的个数

字符串可以包括数字.字母.汉字或者其他字符.使用Charater类的isDigit()方法可以判断字符串中的某个字符是否为数字, 使用Character类的isLetter()方法可以判断字符串中的某个字符是否为字母. 本案例将介绍用"正则表达式"来判断字符串中的某个字符是否为汉字,并统计该字符串中汉字的数量. 关键技术: Java中提供Pattern用于正则表达式的编译方式,该类的静态方法matches()可以执行正则表达式的匹配.该方法的声明如下: public static bo

问题 C: c#统计字符串中数字字符的个数

题目描述 假设有一个GetNumber方法(参数为字符串strSource),编写一个静态方法可以用来统计字符串strSource中数字字符的个数. 输入 输入一个字符串strSource 输出 strSource字符串中数字字符的个数 样例输入 .wrapper {position: relative;} #input {position: absolute;top: 0;left: 0;opacity: 0;z-index: -10;} copy asffkl8asjkfjklas3jdf9

计算一个字符串中大写字母、小写字母、特殊字符、数字的个数

1 public class Test_123_Test { 2 public static void main(String[] args) { 3 String str = "[email protected]#¥%……&"; 4 int bigs = 0;// 记录大写字母的个数 5 int smalls = 0;// 记录小写字母的个数 6 int others = 0;// 记录其他字符的个数 7 int shuzi = 0; 8 System.out.println

Java 练习:求指定字符串中大写字母,小写字母,其他字符分别的个数。

/* public class Test1{ public static void main(String[]args){ String s = "abcdeEFHDKEI38475 "; char a[] = s.toCharArray(); int lower = 0,upper = 0,other = 0; for(int i=0; i<a.length; i++){ if(a[i]<='z' && a[i]>='a') lower++; els

统计字符串中单词的个数

1.单纯统计单词个数,单词与单词之间只考虑空格的情况 // word_statistic.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream> #include <string> using namespace std; #define M 10000 #define N 20 int _tmain(int argc, _TCHAR* argv[]) { char str1[M]={0};

统计字符串中大写、小写、数字的个数(含遍历)

字符串遍历可以用字符串转换方法中的toolCharArray():把字符串转换为字符数组.

关于统计字符串中重复字符个数

一般使用map集合的键不唯一来统计 map.containsKey(b)//判断map集合键中是否包含b 若果不包含就将b作为键存入集合中值为1 map.put(b,1); 如果键中包含那么键不变,值在原来的基础上加1 map.put(b,map.get(key)+1); 代码展现 for(Character key : keySet){ if(!map.contiansKey(b)){ map.put(b,1); }else{ map.put(b,map.get(key)+1); } }