c语言用函数写字符串压缩
时间: 2024-06-14 14:03:39 浏览: 16
在C语言中,可以使用函数来实现字符串压缩。下面是一个示例代码:
```c
#include <stdio.h>
#include <string.h>
void compressString(char* str) {
int len = strlen(str);
int count = 1;
int i;
for (i = 0; i < len; i++) {
if (str[i] == str[i + 1]) {
count++;
} else {
printf("%c%d", str[i], count);
count = 1;
}
}
}
int main() {
char str[] = "abbbcdd";
compressString(str);
return 0;
}
```
这个示例代码中的`compressString`函数接受一个字符串作为参数,并将压缩后的字符串打印出来。它使用一个循环来遍历字符串,通过比较当前字符和下一个字符是否相同来判断是否连续重复出现。如果相同,则计数器`count`加1;如果不同,则打印当前字符和计数器的值,并将计数器重置为1。
运行上述代码,输出结果为`a1b3c1d2`,即将连续重复出现的字符进行了压缩。
相关问题
字符串压缩C语言指针函数
下面是一个简单的字符串压缩 C 语言指针函数的示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* compress(char* str) {
int len = strlen(str);
if (len < 2) {
return str;
}
char* result = (char*) malloc(len + 1);
int count = 1;
char last = str[0];
int j = 0;
for (int i = 1; i <= len; i++) {
if (str[i] == last) {
count++;
} else {
result[j++] = last;
if (count > 1) {
char buf[16];
sprintf(buf, "%d", count);
int k = 0;
while (buf[k] != '\0') {
result[j++] = buf[k++];
}
count = 1;
}
last = str[i];
}
}
result[j] = '\0';
return result;
}
int main() {
char str[] = "abbccccccde";
char* result = compress(str);
printf("%s\n", result);
free(result);
return 0;
}
```
该函数接受一个指向字符串的指针,并返回压缩后的字符串。函数的实现过程是遍历字符串,计算连续相同字符的数量,将字符和数量存储在一个新的字符串中。如果字符只出现一次,则只存储该字符,否则存储字符和数量。最后,返回压缩后的字符串指针。
使用该函数的示例代码在主函数中给出。运行该程序将输出 "ab2c6de",即字符串 "abbccccccde" 的压缩形式。注意,在使用完返回的字符串指针后,应该释放内存以避免内存泄漏。
C语言字符串压缩算法
基于哈夫曼编码的字符串压缩算法是一种常见的字符串压缩算法,下面是C语言实现的步骤和代码示例:
1. 定义结构体Node,用于构建哈夫曼树:
```c
typedef struct Node {
char ch; // 字符
int freq; // 字符出现的频率
struct Node *left, *right; // 左右子节点
} Node;
```
2. 定义函数getFreq(),用于统计字符串中每个字符出现的频率:
```c
int *getFreq(char *str) {
int *freq = (int *) calloc(256, sizeof(int)); // 初始化为0
while (*str) {
freq[(int) *str++]++;
}
return freq;
}
```
3. 定义函数createNode(),用于创建一个新的节点:
```c
Node *createNode(char ch, int freq) {
Node *node = (Node *) malloc(sizeof(Node));
node->ch = ch;
node->freq = freq;
node->left = node->right = NULL;
return node;
}
```
4. 定义函数getMinNode(),用于从节点数组中找到频率最小的两个节点:
```c
void getMinNode(Node **nodes, int n, int *min1, int *min2) {
*min1 = *min2 = -1;
for (int i = 0; i < n; i++) {
if (nodes[i] == NULL) continue;
if (*min1 == -1 || nodes[i]->freq < nodes[*min1]->freq) {
*min2 = *min1;
*min1 = i;
} else if (*min2 == -1 || nodes[i]->freq < nodes[*min2]->freq) {
*min2 = i;
}
}
}
```
5. 定义函数buildTree(),用于构建哈夫曼树:
```c
Node *buildTree(char *str) {
int *freq = getFreq(str);
Node **nodes = (Node **) malloc(sizeof(Node *) * 256);
int n = 0;
for (int i = 0; i < 256; i++) {
if (freq[i] > 0) {
nodes[n++] = createNode((char) i, freq[i]);
}
}
while (n > 1) {
int min1, min2;
getMinNode(nodes, n, &min1, &min2);
Node *node = createNode('\0', nodes[min1]->freq + nodes[min2]->freq);
node->left = nodes[min1];
node->right = nodes[min2];
nodes[min1] = node;
nodes[min2] = NULL;
n--;
}
Node *root = nodes[0];
free(freq);
free(nodes);
return root;
}
```
6. 定义函数getCode(),用于获取每个字符的哈夫曼编码:
```c
void getCode(Node *node, char *code, int len) {
if (node == NULL) return;
if (node->left == NULL && node->right == NULL) {
printf("%c: %s\n", node->ch, code);
return;
}
code[len] = '0';
getCode(node->left, code, len + 1);
code[len] = '1';
getCode(node->right, code, len + 1);
}
```
7. 定义函数compress(),用于压缩字符串:
```c
char *compress(char *str, Node *root) {
char *result = (char *) malloc(sizeof(char) * strlen(str) * 8 + 1);
int len = 0;
while (*str) {
Node *node = root;
while (node->left != NULL && node->right != NULL) {
if (*str == '0') {
node = node->left;
} else {
node = node->right;
}
str++;
}
result[len++] = node->ch;
}
result[len] = '\0';
return result;
}
```
8. 编写main()函数进行测试:
```c
int main() {
char *str = "hello,world!";
Node *root = buildTree(str);
getCode(root, (char *) malloc(sizeof(char) * 100), 0);
char *result = compress(str, root);
printf("Original string: %s\n", str);
printf("Compressed string: %s\n", result);
return 0;
}
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![dll](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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)