【C程序】字符串拆分子串传入传出接口函数×2
来源:互联网 发布:linux mv 文件夹 编辑:程序博客网 时间:2024/06/04 00:37
/* 优点:效率高,纯指针操作 */#include <stdio.h>#include <ctype.h>#include <string.h>#define LEN 10static int split_string (char *src_str, char dest_str[][LEN]){int index = 0;char *ph = src_str;char *p = src_str;if(!src_str)return -1;/* *p != NULL && *p != 空格 */while(*p && isspace(*p) == 0){if(index > 9)return -1;if(*(++p) == ',' || *p == '\0' || isspace(*p)){memcpy(dest_str[index], ph, (p - ph));index++;ph = p+1;//printf ("dest_str[%d]=%s.\n",index-1,dest_str[index-1]);}}return 0;}int main (void){char *p = "111,222,333,aaa,bbb,ccc";char test_buf[LEN][LEN] = {0};int i = 0;split_string (p, test_buf);for (i = 0; i < 10 && strlen (test_buf[i]); i++)printf ("test_buf[%d] = %s\n", i, test_buf[i]);return 0;}
/* 优点:分隔符可通过参数传递 */#include <stdio.h>#include <ctype.h>#include <string.h>#define LEN 10static int strtok_string (char *src_str, char dest_src[][LEN], char *delim){int i = 0;char *p = NULL;p = strtok (src_str, delim);snprintf (dest_src[i], strlen (p)+1, "%s", p);while (p = strtok (NULL, delim)) {i++;snprintf (dest_src[i], strlen (p)+1, "%s", p);}return 0;}int main (void){char s[] = "111,222,333,aaa,bbb,ccc";char *delim = ",";char test_buf[LEN][LEN] = {0};int i = 0;strtok_string (s, test_buf, delim); // test_buf type: char (*)[]for (i = 0; i < LEN && strlen (test_buf[i]); i++)printf ("test_buf[%d] = %s\n", i, test_buf[i]);return 0;}
验证效果:
阅读全文
0 0
- 【C程序】字符串拆分子串传入传出接口函数×2
- C语言的本质(16)——函数接口的传入参数与传出参数
- com接口传入、传出字符串的说明和例程
- c# 调用 C++ dll 传入传出 字符串
- c# 调用 C++ dll 传入传出 字符串
- c# 调用 C++ dll 传入传出 字符串
- 函数之传入参数,传出参数
- 函数的传入和传出参数
- python用ctypes调用C库文件,并且实现字符串传入传出。。。。。
- 函数返回多个值,传入参数和传出参数
- C语言字符串拆分函数strtok
- C语言字符串拆分函数strtok
- 传入传出参数
- GetSubString获取取指定子串和拆分字符串的函数GetSubStr
- C#调用C++dll 传出字符串
- C/C++: 字符串拆分
- C语言版字符串查找函数,字符串中查找子串
- 配置 Sharepoint传入/传出电子邮件
- extern "C"在DLL导出函数时有什么作用?
- nsq源码分析backend_queue.go
- zabbix3.2监控Oracle11G数据脚本
- 如何在网页标题栏title加入logo图标?
- mysql 5.6.24免安装版配置详解
- 【C程序】字符串拆分子串传入传出接口函数×2
- hihocoder 1185(SCC,路径和)
- C 基础
- 第五节总结
- datastage作业hang住
- poj1470的输入
- NSQ数据流模型结构简介
- selinux的初级管理
- AR涂涂乐项目之识别图制作制作地球仪线框一