遍历联合里面的字符串并匹配的C代码
时间: 2023-02-08 19:59:02 浏览: 60
这是一个遍历字符串联合并匹配的示例代码:
```c
#include <stdio.h>
#include <string.h>
union data {
int i;
float f;
char str[20];
} data;
int main() {
union data data;
// 初始化字符串
strcpy(data.str, "hello");
// 遍历字符串
for (int i = 0; i < strlen(data.str); i++) {
printf("%c\n", data.str[i]);
}
// 匹配字符串
if (strcmp(data.str, "hello") == 0) {
printf("match\n");
} else {
printf("not match\n");
}
return 0;
}
```
请注意,联合变量只能存储其中一个成员的值。因此,如果您在联合中存储了字符串,则将不能再使用其他成员来存储值。
相关问题
遍历枚举里面的字符串并匹配的C代码
以下是一个示例代码,它遍历一个字符串数组,并在每个字符串中查找一个特定的字符串:
```c
#include <stdio.h>
#include <string.h>
int main() {
char *strings[] = {"apple", "banana", "cherry"};
char search[] = "a";
int i, j;
for (i = 0; i < sizeof(strings) / sizeof(strings[0]); i++) {
for (j = 0; j < strlen(strings[i]); j++) {
if (strings[i][j] == search[0]) {
printf("Found '%c' in '%s' at index %d\n", search[0], strings[i], j);
break;
}
}
}
return 0;
}
```
该代码遍历字符串数组`strings`,并在每个字符串中查找字符串`search`。如果找到匹配项,则在屏幕上打印该字符串和其索引。
C语言把树的遍历转存入字符串数组
在C语言中,可以通过递归实现树的遍历,并将遍历结果存入字符串数组中。以下是一个示例程序,它将二叉树的中序遍历结果存入字符串数组中:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_SIZE 100
// 二叉树结构体
typedef struct TreeNode {
int data;
struct TreeNode *left;
struct TreeNode *right;
} TreeNode;
// 创建二叉树
TreeNode* createTree() {
int data;
scanf("%d", &data);
if (data == -1) {
return NULL;
}
TreeNode *root = (TreeNode*)malloc(sizeof(TreeNode));
root->data = data;
root->left = createTree();
root->right = createTree();
return root;
}
// 中序遍历二叉树,并将结果存入字符串数组中
void inorderTraversal(TreeNode *root, char **strings, int *index) {
if (root == NULL) {
return;
}
inorderTraversal(root->left, strings, index);
char str[MAX_SIZE];
sprintf(str, "%d", root->data);
strings[*index] = (char*)malloc(strlen(str) + 1);
strcpy(strings[*index], str);
(*index)++;
inorderTraversal(root->right, strings, index);
}
int main() {
TreeNode *root = createTree();
char *strings[MAX_SIZE];
int index = 0;
inorderTraversal(root, strings, &index);
printf("Inorder Traversal: ");
for (int i=0; i<index; i++) {
printf("%s ", strings[i]);
}
printf("\n");
return 0;
}
```
在该程序中,我们使用 `createTree` 函数创建一个二叉树。然后,我们定义了一个字符串数组 `strings`,用于存储遍历结果。在 `inorderTraversal` 函数中,我们使用递归实现了中序遍历,并将遍历结果存入字符串数组中。最后,我们使用 `printf` 函数输出遍历结果。注意,在程序结束时,需要手动释放字符串数组中每个字符串的内存。
相关推荐
![bat](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)