用malloc字符串翻转
时间: 2023-12-01 15:42:45 浏览: 36
下面是使用malloc函数翻转字符串的示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* reverse(char* str) {
int len = strlen(str);
char* rev = (char*)malloc(len + 1);
for (int i = 0; i < len; i++) {
rev[i] = str[len - i - 1];
}
rev[len] = '\0';
return rev;
}
int main() {
char* str = "hello world";
char* rev = reverse(str);
printf("%s\n", rev); // 输出:dlrow olleh
free(rev);
return 0;
}
```
该示例中,我们使用了malloc函数动态分配了一个新的字符串数组,然后使用for循环将原字符串中的字符逆序复制到新的字符串数组中,最后在新字符串数组的末尾添加了一个空字符'\0'。最后,我们释放了动态分配的内存。
相关问题
C++ 使用queue和stack翻转字符串里面的单词 代码
我可以回答这个问题。使用queue和stack翻转字符串里面的单词的代码如下:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define MAX_LEN 100
void reverse_words(char *str);
int main() {
char str[MAX_LEN];
printf("请输入一个字符串:");
fgets(str, MAX_LEN, stdin);
reverse_words(str);
printf("翻转后的字符串为:%s", str);
return ;
}
void reverse_words(char *str) {
int len = strlen(str);
char *p = str;
char *q = str;
char *word_start = NULL;
char *word_end = NULL;
char *temp = NULL;
int i;
// 去掉字符串开头的空格
while (*p && isspace(*p)) {
p++;
}
// 如果整个字符串都是空格,则直接返回
if (!*p) {
return;
}
// 翻转单词
while (*p) {
// 找到单词的起始位置
if (!word_start && !isspace(*p)) {
word_start = p;
}
// 找到单词的结束位置
if (word_start && (isspace(*p) || *p == '\n')) {
word_end = p - 1;
}
// 翻转单词
if (word_start && word_end) {
temp = (char *)malloc(word_end - word_start + 2);
for (i = ; word_start + i <= word_end; i++) {
temp[i] = *(word_start + i);
}
temp[i] = '\';
while (i--) {
*q++ = temp[i];
}
free(temp);
word_start = NULL;
word_end = NULL;
}
p++;
}
// 去掉字符串末尾的空格
while (q > str && isspace(*(q - 1))) {
q--;
}
// 添加字符串结束符
*q = '\';
// 翻转整个字符串
q = str;
while (*q) {
q++;
}
q--;
while (p < q) {
char temp = *p;
*p++ = *q;
*q-- = temp;
}
}
```
统计字符串出现的次数并输出哈夫曼编码c语言
这里提供一个使用哈希表统计字符串出现次数的C语言示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_HASH_TABLE_SIZE 100 // 哈希表的最大长度
typedef struct node {
char* key; // 键值
int value; // 出现次数
struct node* next; // 指向下一个节点的指针
} Node;
Node* hashTable[MAX_HASH_TABLE_SIZE]; // 哈希表
int tableSize = 0; // 实际哈希表的长度
// 哈希函数
int hash(char* key) {
int hashValue = 0;
while (*key != '\0') {
hashValue = (hashValue << 5) + *key++; // 每个字符左移5位再相加
}
return hashValue % MAX_HASH_TABLE_SIZE;
}
// 插入一个节点到哈希表中
void insert(char* key) {
int index = hash(key);
Node* ptr = hashTable[index];
while (ptr != NULL) {
if (strcmp(ptr->key, key) == 0) { // 如果找到了相同的键值,增加出现次数
++ptr->value;
return;
}
ptr = ptr->next;
}
// 如果没有找到相同的键值,创建一个新的节点并插入到链表头部
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->key = key;
newNode->value = 1;
newNode->next = hashTable[index];
hashTable[index] = newNode;
++tableSize;
}
// 遍历哈希表,输出统计结果
void printHashTable() {
printf("Result (key, value):\n");
for (int i = 0; i < MAX_HASH_TABLE_SIZE; ++i) {
Node* ptr = hashTable[i];
while (ptr != NULL) {
printf("('%s', %d)\n", ptr->key, ptr->value);
ptr = ptr->next;
}
}
}
int main() {
// 读入字符串
char str[100];
printf("Please input the string:\n");
fgets(str, sizeof(str), stdin);
str[strlen(str)-1] = '\0'; // 去掉换行符
// 把字符串按照空格分割成多个单词
char* word = strtok(str, " ");
while (word != NULL) {
insert(word);
word = strtok(NULL, " ");
}
printHashTable();
return 0;
}
```
关于哈夫曼编码,可以参考下面的C语言示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_NODE_NUM 200 // 最大节点数
#define MAX_CODE_LEN 100 // 最大编码长度
// 哈夫曼树的节点
typedef struct node {
int weight; // 权值
int parent, lchild, rchild; // 指向父节点和左右子节点的指针
} HNode;
// 编码表的一个项
typedef struct code {
char data; // 字符
char code[MAX_CODE_LEN]; // 对应的哈夫曼编码
} HCode;
// 创建哈夫曼树
void createHuffmanTree(HNode* tree, int n) {
for (int i = 0; i < 2*n-1; ++i) {
tree[i].parent = -1;
tree[i].lchild = -1;
tree[i].rchild = -1;
}
for (int i = 0; i < n; ++i) {
printf("Please input the weight of node %d:\n", i+1);
scanf("%d", &(tree[i].weight));
}
for (int i = n; i < 2*n-1; ++i) {
int s1 = -1, s2 = -1; // 找到权值最小的两个节点
for (int j = 0; j < i; ++j) {
if (tree[j].parent == -1) {
if (s1 == -1 || tree[j].weight < tree[s1].weight) {
s2 = s1;
s1 = j;
} else if (s2 == -1 || tree[j].weight < tree[s2].weight) {
s2 = j;
}
}
}
tree[s1].parent = i;
tree[s2].parent = i;
tree[i].lchild = s1;
tree[i].rchild = s2;
tree[i].weight = tree[s1].weight + tree[s2].weight;
}
}
// 从叶子节点遍历哈夫曼树,生成编码表
void createHuffmanCode(HNode* tree, HCode* hcodes, int n) {
char buffer[MAX_CODE_LEN]; // 缓冲区
for (int i = 0; i < n; ++i) {
int current = i;
int parent = tree[current].parent;
int codeLen = 0;
while (parent != -1) { // 遍历到根节点
if (tree[parent].lchild == current) { // 左子节点,则在编码前面添加0
buffer[codeLen++] = '0';
} else if (tree[parent].rchild == current) { // 右子节点,则在编码前面添加1
buffer[codeLen++] = '1';
}
current = parent;
parent = tree[current].parent;
}
buffer[codeLen] = '\0';
hcodes[i].data = (char)(i+1); // 字符,这里设为i+1
strcpy(hcodes[i].code, strrev(buffer)); // 将缓冲区中的0和1翻转,得到正确的编码
}
}
int main() {
// 创建哈夫曼树
HNode* tree = (HNode*)malloc(MAX_NODE_NUM * sizeof(HNode));
printf("Please input the number of leaf nodes:\n");
int n;
scanf("%d", &n);
createHuffmanTree(tree, n);
// 生成哈夫曼编码
HCode* hcodes = (HCode*)malloc(n * sizeof(HCode));
createHuffmanCode(tree, hcodes, n);
// 输出编码表
printf("Huffman coding table:\n");
for (int i = 0; i < n; ++i) {
printf("%c: %s\n", hcodes[i].data, hcodes[i].code);
}
return 0;
}
```
注意:这里用到了一个strrev()函数,请自行实现。