版权声明:转载请注明出处 https://blog.csdn.net/DuanLiuchang/article/details/79818058
统计大写字母个数
题目描述
找出给定字符串中大写字符(即'A'-'Z')的个数
接口说明
原型:int CalcCapital(String str);
返回值:int
输入描述:
输入一个String数据
输出描述:
输出string中大写字母的个数
示例1
输入
add123#$%#%#O
输出
1
C语言实现:
#include<stdio.h>
#include<string.h>
#define MAX_SIZE 1024
int main()
{
char str[MAX_SIZE];
while (scanf("%s", &str) != EOF)
{
int num = 0;
int length = strlen(str);
int i = 0;
for (i = 0; i < length; i++)
{
if((str[i] >= 'A') && (str[i] <= 'Z'))
{
num++;
}
}
printf("%d\n", num);
}
}
Java实现
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
while (input.hasNext()) {
String str = input.next();
int count = 0;
for (int i = 0; i < str.length(); i++) {
char a = str.charAt(i);
if (a >= 'A' && a <= 'Z') {
count++;
}
}
System.out.println(count);
}
}
}