通过键盘输入一串小写字母(a到z)组成的字符串。请编写一个字符串压缩程序(函数实现),将字符串中连续出席的重复字母(不超过9个)进行压缩,并输出压缩后的字符串。
时间: 2024-05-01 11:18:41 浏览: 22
好的,这是一个字符串压缩的问题。我们可以在遍历字符串的过程中记录字母出现的次数,如果出现的字母不同或者连续出现的个数超过了9个,就将其压缩后输出。以下是实现这个程序的Python代码:
```
def compress_string(s):
if not s:
return ""
res, count = "", 1
for i in range(1, len(s)):
if s[i] == s[i-1]:
count += 1
else:
res += s[i-1] + str(count) if count <= 9 else s[i-1] + '9'
count = 1
res += s[-1] + str(count) if count <= 9 else s[-1] + '9'
return res
```
现在,请问还有什么问题需要我回答呢?
相关问题
用c语言编写,从键盘输入一个字符串,统计按a到z各个字母的出现字数
OK,请输入代码:
```c
#include <stdio.h>
#include <string.h>
int main() {
char str[100]; // 假设字符串长度不超过100
int len, count[26] = {0}; // count数组存放按a到z的字母出现次数
printf("请输入一个字符串:\n");
fgets(str, sizeof(str), stdin); // 从键盘读入字符串
len = strlen(str); // 获取字符串长度
for (int i = 0; i < len; i++) {
if (str[i] >= 'a' && str[i] <= 'z') { // 判断字符是否为小写字母
count[str[i] - 'a']++; // 字符出现次数加一
}
}
// 输出a到z各字母出现次数
for (int i = 0; i < 26; i++) {
printf("%c:%d\n", 'a' + i, count[i]);
}
return 0;
}
```
如果你想使用更好的观感,可以在个别系统上使用:
```c
#include <stdio.h>
#include <string.h>
int main() {
char str[100]; // 假设字符串长度不超过100
int len, count[26] = {0}; // count数组存放按a到z的字母出现次数
printf("请输入一个字符串:\n");
fgets(str, sizeof(str), stdin); // 从键盘读入字符串
len = strlen(str); // 获取字符串长度
for (int i = 0; i < len; i++) {
if (str[i] >= 'a' && str[i] <= 'z') { // 判断字符是否为小写字母
count[str[i] - 'a']++; // 字符出现次数加一
}
}
// 打印表头
printf("+-----+-----+\n");
printf("|%-5s|%-5s|\n", "字母", "出现次数");
printf("+-----+-----+\n");
// 输出a到z各字母出现次数
for (int i = 0; i < 26; i++) {
printf("|%-5c|%-5d|\n", 'a' + i, count[i]);
printf("+-----+-----+\n");
}
return 0;
}
```
用C语言编写一个程序,统计输入的字符串中大写字母和小写字母的个数
好的,我可以为您提供这个程序的代码。请注意,这个程序只能处理单个输入字符串,如果要处理多个字符串,需要在代码中加入循环。
```c
#include <stdio.h>
int main() {
char input[1000];
int i, upper = 0, lower = 0;
printf("请输入一个字符串:");
fgets(input, 1000, stdin);
for (i = 0; input[i] != '\0'; i++) {
if (input[i] >= 'A' && input[i] <= 'Z')
upper++;
else if (input[i] >= 'a' && input[i] <= 'z')
lower++;
}
printf("大写字母个数:%d\n", upper);
printf("小写字母个数:%d\n", lower);
return 0;
}
```
这个程序会让用户输入一个字符串,并统计其中大写字母和小写字母的个数。程序中使用了一个字符数组 `input` 存储输入的字符串,使用了两个变量 `upper` 和 `lower` 存储大写字母和小写字母的个数。其中,`fgets()` 函数用于读取用户输入的字符串,`for` 循环用于遍历字符串中的每一个字符,如果字符是大写字母则 `upper` 自增,如果是小写字母则 `lower` 自增。最后,程序输出统计结果。
相关推荐
![py](https://img-home.csdnimg.cn/images/20210720083646.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)