c语言编写解析这段字符串将{1231#zhangdi#}中的1231和zhangdi分开
时间: 2024-02-21 16:00:01 浏览: 21
可以使用C语言中的字符串处理函数`strtok()`和`strstr()`来实现。
首先,使用`strstr()`函数在字符串中查找`#`的位置,确定要分开的两个子串的起始位置。然后,使用`strtok()`函数将字符串分割成两个子串。
下面是具体的代码实现:
```c
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "{1231#zhangdi#}";
char *p, *q;
char num[10], name[20];
p = strstr(str, "#"); // 查找第一个#
q = strstr(p + 1, "#"); // 查找第二个#
strncpy(num, str + 1, p - str - 1); // 提取第一个子串
num[p - str - 1] = '\0';
strncpy(name, p + 1, q - p - 1); // 提取第二个子串
name[q - p - 1] = '\0';
printf("num: %s\n", num);
printf("name: %s\n", name);
return 0;
}
```
运行结果为:
```
num: 1231
name: zhangdi
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![xlsx](https://img-home.csdnimg.cn/images/20210720083732.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)