串操作
来源:互联网 发布:印尼格斗术软件 编辑:程序博客网 时间:2024/06/05 04:08
#include <stdio.h>#include <stdlib.h>#include <string.h>#include "string_lib.h"/*拷贝src到dst,dst[0]为src长度*/int str_assign(STRING *dst, char *src){ int i = 0; if(NULL == src) { return -1; } if(dst.string != NULL) { free(dst->string); dst.string = NULL; } while(src[i] != '\0' && src[i] != '\n') { i++; } if(0 == i) { dst->length = 0; dst->string = NULL; } else { dst->string = (char *)malloc(i + 1); if(NULL == dst->string) { return -1; } memcpy(dst->string, src, i); dst->length = i; } return 0;}/* 由串S复制得串T */ int str_copy(STRING *dst, STRING src){ return 0;}/*判断字符串是否为空*/BOOL is_str_empty(STRING str){ if(0 == str.length) { return TRUE; } else { return FALSE; }}/*获取字符串长度*/int str_length(STRING str){ return str.length;}/*比较字符串,相等返回0*/int str_compare(STRING dst, STRING src){ int i = 0; for(i = 0; i < dst.length && i < src.length; i++) { if(dst.string[i] != src.string[i]) { return dst.string[i] - src.string[i]; } } return dst.length - src.length;}/*释放字符串的内存*/int destroy_str(STRING *str){ if(NULL == str) { return -1; } if(str->string != NULL) { free(str->string); dst->string = NULL; str->length = 0; } return 0;}/*字符串拼接*/int string_cat(STRING *dst, STRING src1, STRING src2){ if(NULL == dst) { return -1; } if(dst->string != NULL) { free(dst->string); dst->string = NULL; dst->length = 0; } dst->string = (char *)malloc(src1.length + src2.length + 1); if(NULL == dst->string) { return -1; } dst->length = src1.length + src2.length; memcpy(dst->string, src1.string, src1.length); memcpy(dst->string + src1.length, src2.string, src2.length); return 0;}/*从src中的pos地方开始获取len长度的字符串到sub中*/int string_sub(STRING *sub, STRING src, int pos, int len){ if(NULL == sub) { return -1; } if(sub->string != NULL) { free(sub->string); sub->length = 0; } if(pos >= src.length || pos <= 0 || pos + len > src.length + 1 || len < 0) { return -1; } sub->string = (char *)malloc(len + 1); if(NULL == sub->string) { return -1; } memcpy(sub->string, src.string + pos, len); sub->length = len; return 0;}/*获取dst中pos位置开始的地方和src长度一样的位置*/int str_index(STRING dst, STRING src, int pos){ int dst_len = 0; int src_len = 0; int i = pos; STRING sub; memset(&sub, 0, sizeof(STRING)); dst_len = str_length(dst); src_len = str_length(src); while(i < dst_len - src_len + 1) { /*截取一段字符串*/ string_sub(&sub, dst, i, src_len); /*比较*/ if(0 == str_compare(sub, src)) { return i; } else { i++; } } return -1;} typedef struct{ char *string; int length;}STRING;int str_copy(STRING *dst, STRING src);BOOL is_str_empty(STRING str);int str_length(STRING str);int str_compare(STRING dst, STRING src);int clear_str(STRING *str);int string_cat(STRING *dst, STRING src1, STRING src2);int string_sub(STRING *sub, STRING src, int pos, int len);int str_assign(STRING *dst, char *src);
0 0
- 串操作
- 串操作
- 串操作
- 串操作
- 串操作
- 串操作
- 串操作
- 串和串操作
- 串操作指令
- 串操作应用举例
- 串定义与操作
- 串操作指令
- 串操作类指令
- 字段串操作
- CLD及串操作
- 串操作程序实验
- 串的相关操作
- JSON串操作
- Exception in thread "main" java.lang.NoSuchMethodError: org.apache.commons.digester.Digester.parse(L
- LibreOffice 源码编译
- 解释器模式
- Redis Cluster原理 - emailed
- 在WPF中嵌入winform控件
- 串操作
- android AudioManager类 详解
- Mariana: Tencent Deep Learning Platform and its Application学习
- win7安装virtualbox遇到的问题
- MySQL提示:The server quit without updating PID file
- OpenStack云计算实战手册(第2版)
- 2015年3月记录
- 平衡二叉树的实现原理
- Restlet core 核心包之Security package. Part 2.