自己實現的C語言string.h 標頭檔的字串函數與幾個記憶體操作函數

來源:互聯網
上載者:User

 

#include<stdio.h>

char* strcpy(char* dest,const char* src);  //字串拷貝
char* strcat(char* dest,const char* src);  //字串串連,返回dest字串
char* strncat(char* dest, const char* src, int size);//把src前n個字串串連到dest,返回dest字串
int   strcmp(const char* src1,const char* src2);  //字串比較  傳回值: 1:> 0:= -1<
int   strncmp(const char* src,const char* dst,int size);  //dst前n個字元和src比較傳回值: 1:>  0:=  -1<
int   strlen(const char* src);  //返回字串長度
char* strchr(const char* src, int ch); //字元ch在字串第一次出現的位置,返回出現字元ch位置開始到字串結束位置的指標
char* strrchr(const char* src, int ch); //字元ch在字串最後一次出現的位置,返回出現字元ch位置開始到字串結束位置的指標
char* strstr(const char* src, const char* sub); //字串sub在字串第一次出現的位置,返回出現字串sub位置開始到字串結束位置的指標

//memery copy operate function==================//
void* memcpy(void* dest,const void* src,unsigned int size); //把src的內容拷到dest記憶體裡去,並返回dest指向的記憶體位址
void* memset(void* dst, int ch, unsigned int size); //把dst記憶體中的size大小用使用ch來初始化,並返回dest指向的記憶體位址
int   memcmp(const void* src1, const void* src2, unsigned int size); //比較記憶體中src1與src2中的前size 個字元是否相等,1:>  0:=  -1<  (see strncmp(const char*,const char*,unsigned int))
void* memchr(const void* dst, int ch, unsigned int size); //在記憶體中的某一範圍內,尋找特定的字元ch,並返回指向ch的指標
void* memmove(void* dst, const void* src, unsigned int size); //memmove()與memcpy()一樣都是用來拷貝src所指的記憶體內容前n個位元組到dest所指的地址上。不同的是,當src和dest所指的記憶體地區重疊時,memmove()仍然可以正確的處理,不過執行效率上會比使用memcpy()略慢些.返回指向dest的指標

//memery copy operate function==================//

int main()
{
// printf("the string len=%d/n",strlen("helloWorld")); 

// char dest[20];
// printf("the string is=%s/n",strcpy(dest,"helloWorld"));

// char dest[30]="hello--";
// printf("after connect sub string is=%s/n",strcat(dest,"world"));

// char dest[30]="hello--";
// printf("after connect sub string is=%s/n",strncat(dest,"world_programe",4));
 
// printf("the string compare result is=%d/n",strcmp("hello","aello"));

// printf("the string compare result is=%d/n",strncmp("hello","hello_good",5));

// printf("the char appear position is=%s/n",strchr("hello-r-es-d",'-'));

// printf("the char appear position is=%s/n",strrchr("hel*lo-r*-e*s-d",'*'));
 
// printf("the sub string is=%s/n",strstr("hello_world","lo"));

 char dest[20];
// printf("the sub string is=%s/n",memcpy(dest,"hello..111",sizeof(dest)));
 printf("the sub string is=%s/n",memset(dest,'0',sizeof(dest)));

 return 0;
}

//=========================================================//

int  strlen(const char* src)
{
 const char* p=src;
 while(*p++!='/0');
 return p-src-1;
}

//=========================================================//
char* strcpy(char* dest,const char* src)
{
 if(dest&&src)
 {
  int i=0;
  while((*(dest+i)=*(src+i))!='/0') i++;
  *(dest+i)='/0';
 // return dest;
 }
 return dest;
}

//=========================================================//
char* strcat(char* dest,const char* src)
{
 if(dest&&src)
 {
  int len=strlen(dest);
 // printf("len=%d/n",len);
  int i=0;
  while((*(dest+len+i)=*(src+i))!='/0') i++;
  *(dest+len+i)='/0';
 }
 return dest;
}

//=========================================================//
char* strncat(char* dest, const char* src, int size)
{
 if(dest&&src)
 {
  int len=strlen(dest); 
  int i=0;
  while((i<size)&&((*(dest+len+i)=*(src+i))!='/0')) i++;   
 }
 return dest;
}

//=========================================================//
int strcmp(const char* src1,const char* src2)
{
 int equal;
 int i=0;
 while(!(equal=*(unsigned char*)(src1+i)-*(unsigned char*)(src2+i))&&(*(src1+i))&&(*(src2+i)))
  i++;
 if(equal<0) return -1;
 else if(equal>0) return 1;
 else return 0;
}

//=================比較兩個字串前size 個字串是否相等==========================//
int strncmp(const char* src1,const char* src2,int size)
{
 int equal;
 int i=0;
 while((i<size)&&!(equal=*(unsigned char*)(src1+i)-*(unsigned char*)(src2+i))&&(*(src1+i))&&(*(src2+i)))
  i++;
 if(equal<0) return -1;
 else if(equal>0) return 1;
 else return 0;
}

//=========================================================//
char* strchr(const char* src, int ch)
{
 int i=0;
 while(*(src+i)&&(*(src+i)!=ch)) i++;
 return (char*)(src+i);
}

//=========================================================//
char* strrchr(const char* src, int ch)
{
 int len=strlen(src);
 int i=0;
 while(i<len&&(*(src+len-i)!=ch)) i++;
 return (char*)(src+(len-i));
}

//=========================================================//
char* strstr(const char* src, const char* sub)
{
 if(src&&sub)
 {
  int subLen=strlen(sub);
  int srcLen=strlen(src);
  int nomatch=1;
  int fds=srcLen-subLen+1;
  int i=0;
  if(fds>0) //find counts in the string
   while((nomatch=strncmp(src+i,sub,subLen))&&fds--)//把當前src的指標往後推,直到找到與sub指標相同為止
    i++;
  if(nomatch)
   return 0;
  else
   return (char*)(src+i);
 }
 return 0;
}

//====================memory operate=====================================//
//====================memory operate=====================================//
void* memcpy(void* dest,const void* src,unsigned int size)
{
 if(dest&&src&&size>0)
 {
  int i=0;
     unsigned char* p=(unsigned char*)dest;
  unsigned char* q=(unsigned char*)src;
  while((i<size)&&(*(p+i)=*(q+i)))
   i++;
  return dest;
 } 
 return 0;
}

//=========================================================//
void* memset(void* dst, int ch, unsigned int size)
{
 int i=0;
 unsigned char* p=(unsigned char*)dst;
 while((i<size)&&(*(p+i)=ch))
  i++;
 return dst;
}

//=========================================================//
int  memcmp(const void* src1, const void* src2, unsigned int size)
{

}

//=========================================================//
void* memchr(const void* dst, int ch, unsigned int size)
{

}

void* memmove(void* dst, const void* src, unsigned int count)
{
   void * ret = dst;
   if (dst <= src || (char *)dst >= ((char *)src + count))
   {
      while (count--)
      {
        *(char *)dst = *(char *)src;
            dst = (char *)dst + 1;
            src = (char *)src + 1;
        }
   }
   else
   {
      dst = (char *)dst + count - 1;
      src = (char *)src + count - 1;
      while (count--)
      {
        *(char *)dst = *(char *)src;
        dst = (char *)dst - 1;
        src = (char *)src - 1;
      }
   }

   return(ret);

}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.