程序博客网 > js根据url打开新页面
来源:互联网 发布:js根据url打开新页面 编辑:程序博客网 时间:2024/04/30 14:50
str.h
#ifndef _STR_H#define _STR_Htypedef struct _str{char* ch;int length;}STR;STR* str_creat(char* str);int str_destory(STR* s);int str_clear(STR* s);int str_compare(STR* s, STR* t);STR* str_cantact(STR* s, STR* t);STR* str_sub(STR* s, int pos, int len);#endif
str.c
#include "str.h"#include <stdio.h>#include <stdlib.h>STR* str_creat(char* str){int i;STR* s = (STR*)malloc(sizeof(STR));if(s == NULL){return NULL;}for(i = 0; str[i]; ++i);s->ch = (char*)malloc((i + 1) * sizeof(char));if(s->ch == NULL){free(s);return NULL;}s->length = i;while(i >= 0){s->ch[i] = str[i];--i;}return s;}int str_destory(STR* s){free(s->ch);free(s);return 0;}int str_clear(STR* s){free(s->ch);s->ch = NULL;s->length = 0;}int str_compare(STR* s, STR* t){int i;for(i = 0; i < s->length && i < t->length; i++){if(s->ch[i] != t->ch[i]){return s->ch[i] - t->ch[i];}}return s->length - t->length;}STR* str_cantact(STR* s, STR* t){int i;char* temp = NULL;temp = (char*)malloc((s->length + t->length + 1) * sizeof(char));if(temp == NULL){return NULL;}for(i = 0; i < s->length; i++){temp[i] = s->ch[i];}for(i = 0; i < t->length; i++){temp[i + s->length] = t->ch[i];}temp[s->length + t->length] = 0;i = s->length + t->length;str_clear(s);s->ch = temp;s->length = i;return s;}STR* str_sub(STR* s, int pos, int len){STR* t = NULL;if(pos < 1 || pos > s->length || len < 0 || len > s->length - pos +1){return NULL;}t = str_creat("");str_clear(t);t->ch = (char*)malloc((len + 1) * sizeof(char));if(t->ch == NULL){free(t);return NULL;}t->length = len;for(--len; len >= 0; --len){t->ch[len] = s->ch[pos - 1 + len];}t->ch[t->length] = 0;return t;}
main.c
#include "str.h"#include <stdio.h>int main(int argc, char* argv[]){int res;STR* s = str_creat("hello");STR* t = str_creat("hello");printf("string = %s, length = %d/n", s->ch, s->length);if((res = str_compare(s,t)) == 0){printf("s = t/n");}else if(res > 0){printf("s > t/n");}else if(res < 0){printf("s < t/n");}str_clear(t);t = str_creat(" world");str_cantact(s, t);printf("string = %s, length = %d/n", s->ch, s->length);str_destory(t);t = str_sub(s, 2, 5);printf("string = %s, length = %d/n", t->ch, t->length);str_destory(s);return 0;}