java统计字符数组c中小写字母的个数,并将它们变成大写字母
时间: 2024-05-03 07:17:09 浏览: 20
可以使用Java中的字符函数以及循环来实现统计并转换字符数组中小写字母为大写字母的功能。以下是示例代码:
```java
public static void countLowerCaseAndConvertToUpperCase(char[] array) {
int count = 0;
for (int i = 0; i < array.length; i++) {
if (Character.isLowerCase(array[i])) {
count++;
array[i] = Character.toUpperCase(array[i]);
}
}
System.out.println("小写字母个数为:" + count);
}
```
在上面的代码中,我们首先定义了一个计数器变量`count`,用于统计字符数组中小写字母的个数。然后使用`for`循环遍历整个字符数组,对于每一个字符,如果它是小写字母,就将计数器加1,并将它转换为大写字母。最后输出小写字母的个数即可。
可以在调用该方法时传入一个字符数组,例如:
```java
char[] array = {'a', 'B', 'c', 'D', 'e', 'F'};
countLowerCaseAndConvertToUpperCase(array);
```
输出结果应为:
```
小写字母个数为:3
```
相关问题
java统计字符串中大写字母,小写字母和数字的个数
可以使用正则表达式和Java中的String类提供的方法来实现统计字符串中大写字母,小写字母和数字的个数。
示例代码:
```
public static void count(String str) {
int upperCaseCount = 0;
int lowerCaseCount = 0;
int numberCount = 0;
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (Character.isUpperCase(c)) {
upperCaseCount++;
} else if (Character.isLowerCase(c)) {
lowerCaseCount++;
} else if (Character.isDigit(c)) {
numberCount++;
}
}
System.out.println("大写字母个数:" + upperCaseCount);
System.out.println("小写字母个数:" + lowerCaseCount);
System.out.println("数字个数:" + numberCount);
}
```
调用示例:
```
String str = "Hello World 123";
count(str);
```
输出结果:
```
大写字母个数:2
小写字母个数:8
数字个数:3
```
C语言:统计输入的一行字符中大写字母小写字母的个数
好的,我明白你的问题了。以下是对应的C语言代码,它可以统计输入的一行字符中大写字母和小写字母的个数:
```c
#include <stdio.h>
int main() {
char line[100];
int uppercase = 0, lowercase = 0, i = 0;
printf("请输入一行字符:");
fgets(line, 100, stdin);
while (line[i] != '\0') {
if (line[i] >= 'A' && line[i] <= 'Z')
uppercase++;
else if (line[i] >= 'a' && line[i] <= 'z')
lowercase++;
i++;
}
printf("大写字母个数为:%d\n", uppercase);
printf("小写字母个数为:%d\n", lowercase);
return 0;
}
```
上面的代码中,我们首先通过`fgets()`函数从标准输入中读取一行字符,并将它存储在`line`数组中。然后,我们使用一个`while`循环遍历`line`数组中的每一个字符,如果当前字符是大写字母,则把`uppercase`计数器加1,如果是小写字母,则把`lowercase`计数器加1。最后,我们输出统计结果。
我希望这个代码对你有所帮助,如果你还有其他问题,请随时提出。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)