字符串压缩解压c语言
时间: 2023-12-09 14:36:18 浏览: 100
字符串解压缩
5星 · 资源好评率100%
根据提供的引用内容,我们可以了解到这是一道关于字符串压缩解压的C语言题目。下面是对该问题的回答:
1. 字符串压缩
```c
#include <stdio.h>
#include <string.h>
int main()
{
char c;
char str[1001];
int count = 1;
scanf("%c\n", &c);
fgets(str, 1001, stdin);
if (c == 'C')
{
for (int i = 0; i < strlen(str); i++)
{
if (str[i] == str[i + 1])
{
count++;
}
else
{
if (count > 1)
{
printf("%d", count);
}
printf("%c", str[i]);
count = 1;
}
}
}
return 0;
}
```
2. 字符串解压
```c
#include <stdio.h>
#include <string.h>
int main()
{
char c;
char str[1001];
int count = 0;
scanf("%c\n", &c);
fgets(str, 1001, stdin);
if (c == 'D')
{
for (int i = 0; i < strlen(str); i++)
{
if (str[i] >= '0' && str[i] <= '9')
{
count = count * 10 + str[i] - '0';
}
else
{
if (count == 0)
{
printf("%c", str[i]);
}
else
{
for (int j = 0; j < count; j++)
{
printf("%c", str[i]);
}
count = 0;
}
}
}
}
return 0;
}
```
阅读全文