c语言String的实现(一)

2013-07-22 17:58:37 · 作者: · 浏览: 512

  /*

  * String.c

  *

  *  Created on: 2012-12-4

  *      Author: Administrator

  */

  #include <stdio.h>

  #include <stdlib.h>

  #define TRUE 1

  #define FALSE 0

  #define CHECK(t) do{if(!(t))return 0;}while(0)

  typedef char* String;

  typedef int Status;

  static String STRBUFFER = 0;

  int StrLen(String buf){

  String temp = buf;

  while(*temp++);

  return temp-buf-1;

  }

  String StrMake(int length){

  return (String)malloc(length+1);

  }

  String StrChr(String buf, char ch, int length){

  while(length--&&*buf++ != ch);

  return buf-1;

  }

  void StrCat(String *dest,String src){

  String temp = (String)malloc(StrLen(*dest)+StrLen(src)+1);

  String head = temp;

  String d = *dest;

  while(*d){

  *temp++ = *d++;

  }

  while(*src){

  *temp++ = *src++;

  };

  *temp = '\0';

  *dest = head;

  }

  int StrCmp(String buf1,String buf2){

  do{

  if(*buf1>*buf2)return 1;

  if(*buf1<*buf2)return -1;

  if(!*buf1)break;

  buf1++;

  buf2++;

  }while(1);

  return 0;

  }