输入一个字符串(包含大小写字母以及数字字符和空格) ①求字符串中的数字和 ②大写字母和小写字母数量之和 ③将字符串中所有的空格替换成* 之后输出
时间: 2024-02-12 12:02:16 浏览: 114
好的,下面是Python的实现:
```python
s = input("请输入一个字符串:")
# 求字符串中的数字和
digits_sum = 0
for char in s:
if char.isdigit():
digits_sum += int(char)
print("字符串中的数字和为:", digits_sum)
# 大写字母和小写字母数量之和
upper_count = 0
lower_count = 0
for char in s:
if char.isupper():
upper_count += 1
elif char.islower():
lower_count += 1
print("大写字母和小写字母数量之和为:", upper_count + lower_count)
# 将字符串中所有的空格替换成* 之后输出
s = s.replace(' ', '*')
print("替换后的字符串为:", s)
```
示例输入:
```
请输入一个字符串:AaBbCc 123 456
```
输出结果:
```
字符串中的数字和为: 21
大写字母和小写字母数量之和为: 6
替换后的字符串为: AaBbCc*123*456
```
相关问题
c语言输入一个字符串(包含大小写字母以及数字字符和空格) ①求字符串中的数字和 ②大写字母和小写字母数量之和 ③将字符串中所有的空格替换成* 之后输出
好的,下面是C语言的实现:
```c
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
int main() {
char s[1000];
printf("请输入一个字符串:");
fgets(s, 1000, stdin);
// 求字符串中的数字和
int digits_sum = 0;
for (int i = 0; i < strlen(s); i++) {
if (isdigit(s[i])) {
digits_sum += s[i] - '0';
}
}
printf("字符串中的数字和为:%d\n", digits_sum);
// 大写字母和小写字母数量之和
int upper_count = 0;
int lower_count = 0;
for (int i = 0; i < strlen(s); i++) {
if (isupper(s[i])) {
upper_count++;
} else if (islower(s[i])) {
lower_count++;
}
}
printf("大写字母和小写字母数量之和为:%d\n", upper_count + lower_count);
// 将字符串中所有的空格替换成* 之后输出
for (int i = 0; i < strlen(s); i++) {
if (s[i] == ' ') {
s[i] = '*';
}
}
printf("替换后的字符串为:%s\n", s);
return 0;
}
```
示例输入:
```
请输入一个字符串:AaBbCc 123 456
```
输出结果:
```
字符串中的数字和为:21
大写字母和小写字母数量之和为:6
替换后的字符串为:AaBbCc*123*456
```
python输入一个字符串(包括大小写字母和空格),除去空格输出在字符串中出现过的字符。 输入格式 输入一个字符串 输出格式: 输出在字符串中出现过的字符,每个字符打印一次,按字母顺序打印(注意:空格不算,大小写字符算不同字符》。打印时按字母的顺序,大写在前,小写在后。
好的,这是一个简单的字符串处理问题,以下是 Python 的代码实现:
```python
s = input().replace(" ", "") # 去除空格
unique_chars = sorted(set(s)) # 去重并排序
for c in unique_chars:
print(c)
```
这段代码首先读入一个字符串并去除其中的空格,然后使用 Python 的 `set()` 函数去重并排序,最后遍历每个字符并打印输出。需要注意的是,大小写字母算不同字符且按字母顺序打印,因此我们对去重后的字符列表进行了排序。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231045021.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)