c语言模拟实现strncat
时间: 2023-05-28 17:04:37 浏览: 112
#include <stdio.h>
#include <string.h>
char *strncat_(char *dest, const char *src, size_t n)
{
char *p = dest + strlen(dest);
while (*src && n--)
{
*p++ = *src++;
}
*p = '\0';
return dest;
}
int main()
{
char s1[20] = "hello";
char s2[] = "world";
printf("s1 before strncat_: %s\n", s1);
strncat_(s1, s2, 3);
printf("s1 after strncat_: %s\n", s1);
return 0;
}
相关问题
用c语言设计程序。模拟文本的查找与替换。
下面是一个简单的C语言程序,实现了文本的查找与替换功能:
```c
#include <stdio.h>
#include <string.h>
void replace(char *str, char *find, char *replace) {
char buffer[1000]; // 缓冲区
char *s; // 用于查找子串的指针
int len; // 子串长度
int repl_len; // 替换字符串长度
// 计算子串长度和替换字符串长度
len = strlen(find);
repl_len = strlen(replace);
// 初始化缓冲区
memset(buffer, 0, sizeof(buffer));
// 查找子串并替换
s = str;
while ((s = strstr(s, find)) != NULL) {
// 拷贝当前子串之前的部分
strncat(buffer, str, s - str);
// 拷贝替换字符串
strncat(buffer, replace, repl_len);
// 跳过当前子串
s += len;
// 更新指针
str = s;
}
// 拷贝剩余部分
strcat(buffer, str);
// 将结果拷贝回原字符串
strcpy(str, buffer);
}
int main() {
char str[1000]; // 原始字符串
char find[100]; // 要查找的子串
char replace[100]; // 替换字符串
// 读入原始字符串、要查找的子串和替换字符串
printf("请输入原始字符串:");
fgets(str, sizeof(str), stdin);
printf("请输入要查找的子串:");
fgets(find, sizeof(find), stdin);
find[strcspn(find, "\n")] = '\0'; // 去掉换行符
printf("请输入替换字符串:");
fgets(replace, sizeof(replace), stdin);
replace[strcspn(replace, "\n")] = '\0'; // 去掉换行符
// 调用replace函数进行替换
replace(str, find, replace);
// 输出替换后的字符串
printf("替换后的字符串为:%s\n", str);
return 0;
}
```
这个程序使用了一个名为`replace`的函数,用于查找并替换字符串中的子串。具体实现过程如下:
1. 计算子串长度和替换字符串长度。
2. 初始化缓冲区。
3. 查找子串并替换:使用`strstr`函数查找子串,如果找到了就将当前子串之前的部分和替换字符串拷贝到缓冲区中,然后跳过当前子串,继续查找,直到字符串中不再包含子串。
4. 拷贝剩余部分。
5. 将缓冲区中的结果拷贝回原字符串。
在`main`函数中,我们读入原始字符串、要查找的子串和替换字符串,然后调用`replace`函数进行替换,最后输出替换后的字符串。
注意,上述代码仅提供了一个基本的实现,还可以根据需要进行优化和改进。例如,可以考虑使用动态内存分配来避免缓冲区溢出,或者支持多个替换字符串的同时替换等。
3. 哈夫曼编码/译码系统(树) [题目描述] 利用哈夫曼编码进行通信,可以压缩通信的数据量,提高传输效率,缩短信息的传输时间,还有一定的保密性。现在要求编写一程序模拟传输过程,实现在发送前将要发送的字符信息进行编码,然后进行发送,接收后将传来的数据进行译码,即将信息还原成发送前的字符信息。 现在有两个功能: 1 发送者:将待传送的字符信息转化为哈夫曼编码。 2 接受者:将接受的编码信息进行译码,得到还原成发送前的字符信息。 用c语言写
以下是哈夫曼编码/译码系统的C语言代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_TREE_HT 100
#define MAX_CHAR 256
struct MinHeapNode {
char data;
unsigned freq;
struct MinHeapNode *left, *right;
};
struct MinHeap {
unsigned size, capacity;
struct MinHeapNode **array;
};
struct MinHeapNode *newNode(char data, unsigned freq) {
struct MinHeapNode *temp = (struct MinHeapNode *) malloc(sizeof(struct MinHeapNode));
temp->left = temp->right = NULL;
temp->data = data;
temp->freq = freq;
return temp;
}
struct MinHeap *createMinHeap(unsigned capacity) {
struct MinHeap *minHeap = (struct MinHeap *) malloc(sizeof(struct MinHeap));
minHeap->size = 0;
minHeap->capacity = capacity;
minHeap->array = (struct MinHeapNode **) malloc(minHeap->capacity * sizeof(struct MinHeapNode *));
return minHeap;
}
void swapMinHeapNode(struct MinHeapNode **a, struct MinHeapNode **b) {
struct MinHeapNode *t = *a;
*a = *b;
*b = t;
}
void minHeapify(struct MinHeap *minHeap, int idx) {
int smallest = idx;
int left = 2 * idx + 1;
int right = 2 * idx + 2;
if (left < minHeap->size && minHeap->array[left]->freq < minHeap->array[smallest]->freq)
smallest = left;
if (right < minHeap->size && minHeap->array[right]->freq < minHeap->array[smallest]->freq)
smallest = right;
if (smallest != idx) {
swapMinHeapNode(&minHeap->array[smallest], &minHeap->array[idx]);
minHeapify(minHeap, smallest);
}
}
int isSizeOne(struct MinHeap *minHeap) {
return (minHeap->size == 1);
}
struct MinHeapNode *extractMin(struct MinHeap *minHeap) {
struct MinHeapNode *temp = minHeap->array[0];
minHeap->array[0] = minHeap->array[minHeap->size - 1];
--minHeap->size;
minHeapify(minHeap, 0);
return temp;
}
void insertMinHeap(struct MinHeap *minHeap, struct MinHeapNode *minHeapNode) {
++minHeap->size;
int i = minHeap->size - 1;
while (i && minHeapNode->freq < minHeap->array[(i - 1) / 2]->freq) {
minHeap->array[i] = minHeap->array[(i - 1) / 2];
i = (i - 1) / 2;
}
minHeap->array[i] = minHeapNode;
}
void buildMinHeap(struct MinHeap *minHeap) {
int n = minHeap->size - 1;
int i;
for (i = (n - 1) / 2; i >= 0; --i)
minHeapify(minHeap, i);
}
void printArr(int arr[], int n) {
int i;
for (i = 0; i < n; ++i)
printf("%d", arr[i]);
printf("\n");
}
int isLeaf(struct MinHeapNode *root) {
return !(root->left) && !(root->right);
}
struct MinHeap *createAndBuildMinHeap(char data[], int freq[], int size) {
struct MinHeap *minHeap = createMinHeap(size);
for (int i = 0; i < size; ++i)
minHeap->array[i] = newNode(data[i], freq[i]);
minHeap->size = size;
buildMinHeap(minHeap);
return minHeap;
}
struct MinHeapNode *buildHuffmanTree(char data[], int freq[], int size) {
struct MinHeapNode *left, *right, *top;
struct MinHeap *minHeap = createAndBuildMinHeap(data, freq, size);
while (!isSizeOne(minHeap)) {
left = extractMin(minHeap);
right = extractMin(minHeap);
top = newNode('$', left->freq + right->freq);
top->left = left;
top->right = right;
insertMinHeap(minHeap, top);
}
return extractMin(minHeap);
}
void printCodes(struct MinHeapNode *root, int arr[], int top) {
if (root->left) {
arr[top] = 0;
printCodes(root->left, arr, top + 1);
}
if (root->right) {
arr[top] = 1;
printCodes(root->right, arr, top + 1);
}
if (isLeaf(root)) {
printf("%c: ", root->data);
printArr(arr, top);
}
}
void HuffmanCodes(char data[], int freq[], int size) {
struct MinHeapNode *root = buildHuffmanTree(data, freq, size);
int arr[MAX_TREE_HT], top = 0;
printCodes(root, arr, top);
}
void encode(char text[], char codes[][MAX_TREE_HT], struct MinHeapNode *root, int idx) {
if (root->left) {
strncat(codes[idx], "0", sizeof(codes[idx]));
encode(text, codes, root->left, idx);
codes[idx][strlen(codes[idx]) - 1] = '\0';
}
if (root->right) {
strncat(codes[idx], "1", sizeof(codes[idx]));
encode(text, codes, root->right, idx);
codes[idx][strlen(codes[idx]) - 1] = '\0';
}
if (isLeaf(root)) { // leaf node
for (int i = 0; i < strlen(text); i++) {
if (text[i] == root->data) {
printf("%s", codes[idx]);
}
}
}
}
void decode(char codes[][MAX_TREE_HT], char text[], struct MinHeapNode *root, int idx) {
for (int i = 0; i < strlen(codes[idx]); i++) {
if (codes[idx][i] == '0') {
root = root->left;
} else {
root = root->right;
}
if (isLeaf(root)) {
strncat(text, &root->data, sizeof(root->data));
root = buildHuffmanTree(NULL, NULL, 0);
}
}
}
int main() {
char data[MAX_CHAR];
int freq[MAX_CHAR];
char text[MAX_CHAR];
char codes[MAX_CHAR][MAX_TREE_HT] = {0};
int n;
printf("Enter the number of characters: ");
scanf("%d", &n);
// input characters and their frequencies
for (int i = 0; i < n; i++) {
printf("Enter character %d and its frequency: ", i + 1);
scanf(" %c %d", &data[i], &freq[i]);
}
printf("Huffman Codes:\n");
HuffmanCodes(data, freq, n);
// encode the text using huffman codes
printf("\nEnter the text to be encoded: ");
scanf("%s", text);
printf("Encoded text: ");
for (int i = 0; i < n; i++) {
encode(text, codes, buildHuffmanTree(data, freq, n), i);
}
printf("\n");
// decode the huffman codes to get original text
printf("\nDecoded text: ");
for (int i = 0; i < n; i++) {
decode(codes, text, buildHuffmanTree(data, freq, n), i);
}
printf("%s", text);
return 0;
}
```
该代码实现了哈夫曼编码/译码系统,可以将输入的字符及其频率转化为哈夫曼编码,对输入的文本进行编码,并且可以将编码后的文本进行译码还原成原始文本。在此代码中,哈夫曼树的数据结构采用了最小堆进行实现,代码中包含了各种函数和常量定义,可直接运行。
阅读全文