java--String类操作:统计一个字符串中的大写字母,小写字母,数字以及其它字符的个数

本文介绍了一个Java程序,用于统计输入字符串中的大写字母、小写字母、数字和其他字符的数量。通过三个独立的方法实现,分别针对不同类型的字符进行计数。
import java.util.Scanner;

/**
 * 统计一个字符串中的大写字母,小写字母,数字以及其它字符的个数
 * @author Administrator
 *
 */
public class Count_Re {
	
	/**
	 * 统计小写字母
	 * @param str 传进来的原字符串
	 * @return 返回小写字母的个数
	 */
	public int getLowerCaseCount(String str){
		int count = 0;
		
		for(int i = 0; i < str.length(); i++){
			char ch = str.charAt(i);
			if (Character.isLowerCase(ch)) {
				count++;
			}
		}
		
		return count;
	}
	
	/**
	 * 统计大写字母
	 * @param str 传进来的原字符串
	 * @return 返回大写字母的个数
	 */
	public int getUpperCaseCount(String str){
		int count = 0;
		
		for(int i = 0; i < str.length(); i++){
			char ch = str.charAt(i);
			if (Character.isUpperCase(ch)) {
				count++;
			}
		}
		
		return count;
	}
	
	/**
	 * 统计数字
	 * @param str 传进来的原字符串
	 * @return 返回数字的个数
	 */
	public int getDigitCount(String str){
		int count = 0;
		
		for(int i = 0; i < str.length(); i++){
			char ch = str.charAt(i);
			if (Character.isDigit(ch)) {
				count++;
			}
		}
		
		return count;
	}
	
	public static void main(String[] args) {
		System.out.println("请输入字符串:");
		Scanner scanner = new Scanner(System.in);
		String str = scanner.next();
		
		Count_Re count_Re = new Count_Re();
		
		int count = count_Re.getLowerCaseCount(str);
		int count1 = count_Re.getUpperCaseCount(str);
		int count2 = count_Re.getDigitCount(str);
		int count3 = str.length() - count - count1 - count2;
		
		// 输出结果
		System.out.println("字符串" + str);
		System.out.println("大写字母" + count + "个");
		System.out.println("小写字母" + count1+ "个");
		System.out.println("数字" + count2 + "个");
		System.out.println("其他" + count3 + "个");
	}
}

好的,我很乐意为您介绍如何使用C语言编程来统计一个字符串大写字母小写字母数字和其他字符个数。以下是一个实现这个功能的C语言程序: ```c #include <stdio.h> #include <ctype.h> #include <string.h> void countCharacters(const char *str) { int uppercase = 0, lowercase = 0, digits = 0, others = 0; int length = strlen(str); for (int i = 0; i < length; i++) { if (isupper(str[i])) { uppercase++; } else if (islower(str[i])) { lowercase++; } else if (isdigit(str[i])) { digits++; } else { others++; } } printf("统计结果:\n"); printf("大写字母: %d\n", uppercase); printf("小写字母: %d\n", lowercase); printf("数字: %d\n", digits); printf("其他字符: %d\n", others); } int main() { char str[1000]; printf("请输入一个字符串: "); fgets(str, sizeof(str), stdin); // 移除换行符 str[strcspn(str, "\n")] = '\0'; countCharacters(str); return 0; } ``` 这个程序的工作原理如下: 1. 我们定义了一个`countCharacters`函数,它接受一个字符串作为参数。 2. 在这个函数中,我们使用四个变量来分别统计大写字母小写字母数字和其他字符个数。 3. 我们使用`strlen`函数获取字符串的长度,然后遍历字符串中的每个字符。 4. 对于每个字符,我们使用`ctype.h`头文件中提供的函数来判断它的: - `isupper`: 判断字符是否为大写字母 - `islower`: 判断字符是否为小写字母 - `isdigit`: 判断字符是否为数字 5. 根据判断结果,我们增加相应的计数器。 6. 最后,我们打印出统计结果。 7. 在`main`函数中,我们从用户那里获取输入的字符串,并调用`countCharacters`函数进行统计。 这个程序可以有效地统计字符串中各种字符的数量。您可以将它编译运行,并输入任意字符串来查看统计结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值