#ifndef __STRING__H__
#define __STRING__H__
#include
#include
char *strcpy_(char *dest,char *src);
int strcmp_(char *s1,char *s2);
int strlen_(char *s);
char *strcat_(char *dest,char *src);
char *strchr_(char *s,char c);
char *strstr_(char *haystack,char *needle);
#endif
#include"string_.h"
char *strcpy_(char *dest,char *src)
{
char *ptr =dest;
while(*dest++=*src++);
return ptr;
}
int strcmp_(char *s1,char *s2)
{
while(*s1 && *s2 && *s1 == *s2&&s1++&&s2++);
if(!*s1 && !*s2)
return 0;
return *s1 > *s2 ?1:-1;
}
int strlen_(char *s)
{
int i;
for(i=0;*s++ != '\0';i++);
return i ;
}
char *strcat_(char *dest,char *src)
{
char *ptr=dest;
while(*dest && dest++);
while(*dest++=*src++);
return ptr;
}
char *strchr_(char *s,char c)
{
while(*s&&*s!=c&&s++);
if(*s)
return s;
return NULL;
}
char *strstr_(char *haystack,char *needle)
{
char *ptr=NULL;
char *need=needle;
while(*haystack!=0)
{
if(*haystack == *needle)
{
ptr=haystack;
while(*haystack&&*needle&&*haystack==*needle&&haystack++&&needle++);
if(!*needle)
return ptr;
else if(!*haystack)
return NULL;
else
needle = need;
}
haystack++;
}
return NULL;
}
#include
#include
#include
#include "string_.h"
int main()
{
char *str="abcd";
char s1[50] ="aidfvafdcvp";
char s2[50]="afd";
char *m;
m = (char*)malloc(50);
printf("strcpy : s1=%s,\t复制后: %s\n",s1,strcpy(m,s1));
printf("strcpy_: s1=%s,\t复制后: %s\n\n\n",s1,strcpy_(m,s1));
printf("strlen : s1:%s,\t长度: %d\n",s1,strlen(s1));
printf("strlen_: s1:%s,\t长度: %d\n\n\n",s1,strlen_(s1));
printf("strcat : s1=%s,\t s2=%s\t",s1,s2);
printf(" 返回值: %s\n",strcat(s1,s2));
printf("****************** 再次连接s2=“afd” ******************\n");
printf("strcat_: s1=%s,\t s2=%s\t",s1,s2);
printf(" 返回值: %s\n\n\n",strcat_(s1,s2));
printf("strchr : 在s1中查找‘d’\t s1=%s\t 返回值:%s\n",s1,strchr(s1,'d'));
printf("strchr_: 在s1中查找‘d’\t s1=%s\t 返回值:%s\n\n\n",s1,strchr_(s1,'d'));
printf("strcmp: %s\t %s\t 返回值: %d\n",s1,s2,strcmp(s1,s2));
printf("strcmp_:%s\t %s\t 返回值: %d\n\n\n",s1,s2,strcmp_(s1,s2));
printf("strstr : s1=%s\ts2=%s\t 返回值:%s\n",s1,s2,strstr(s1,s2));
printf("strstr_: s1=%s\ts2=%s\t 返回值:%s\n",s1,s2,strstr_(s1,s2));
return 0;
}
阅读(706) | 评论(0) | 转发(0) |