常用字符串函数的封装.c

#include<stdio.h>
char *p =NULL ;
int my_strlen(const char*s) //求字符串长度
{
int i=0;
while(s[i++]) ;
i-- ;
return i ;
}

char * my_strcpy(char*dest,const char*src) //字符串复制
if(src ==NULL||dest == NULL)
return 0;
if(dest == src)
return dest;
char *start = dest ;//保存头地址
while(*dest++ = *src++);
return start ;
}

char *my_strcat(char*dest,const char*src) //将两个字符串连接
{
if(dest ==NULL ||src ==NULL)
return NULL;
char *start = dest ;
while(*dest++);
dest--;
my_strcpy(dest,src);
return start;
}


int my_strcmp(const char*s1,const char*s2) //比较两个字符串
{
for(;*s1&&*s1==*s2;s1++,s2++);//如果s2,s1!=s2就结束
if(*s1>*s2)
return 1 ;
else if(*s1 <*s2)
return -1 ;
else
return 0  ;
}

char *my_strstr(const char *haystack, const char *needle) //字符串查找
{
const char *t1,*t2;
// t1 = haystack ;
// t2 = needle ;
while(*haystack)
{
// for(*t1,*t2;*t1==*t2;t1++,t2++);
for(t1=haystack,t2=needle;*t1&&*t1==*t2;t1++,t2++);
if(*t2 == 0)
return "find" ;
haystack++ ;
}
return "not find" ;
}
char* my_itoa(int m ,char* buf ) //数字转字符串
{
int i,j;
for(i=0;m;i++)
{
buf[i] = m%10+'0';
m /= 10;
}
char tmp;
for(j=0,--i;j<i;j++,i--)
{
tmp = buf[i];
buf[i]=buf[j];
buf[j] = tmp;
}
return buf;
}

int my_atoi(const char * s) //字符串转数字
{
    int m=0,i;
  for(i=0;s[i];i++)
m=m*10+(s[i]-'0');
  return m;
}


int main()
{
int a , b ,c  ;
char m , n ;
char buf1[1024] = "" ;
char buf2[1024] = "" ;
char buf3[1024] = "" ;

#if 1
printf("\nnum to char \n");
    printf ("please input a number for a\na = "); 
scanf("%d",&a) ;
while (getchar()!='\n'); //清空缓存区
   p = my_itoa(a , buf1) ;
printf("%d my_itoa is %s\n\n",a,p) ;
sleep(1);
#endif
#if 1
printf("\nchar to num \n");
    printf ("please input a char for s\na = "); 
scanf("%[^\n]",buf1) ;
while (getchar()!='\n'); //清空缓存区
    a = my_atoi(buf1) ;
printf("my_itoa is %d\n\n",a) ;
sleep(1);
#endif
#if 1
    printf ("connect  s1 to s2\n"); 
    printf ("please input a string for s1\ns1 = "); 
scanf ("%s",buf1);
while (getchar()!='\n'); //清空缓存区
    printf ("please input a string for s2\ns2 = "); 
  scanf ("%s",buf2) ;
while (getchar()!='\n'); //清空缓存区
p = NULL ;
    p = my_strcat(buf1,buf2);
printf("the new string is %s\n\n",p);
sleep(1);
#endif
#if 1
    printf ("get lenth\n"); 
    printf ("please input a string for s1\ns1 = "); 
scanf ("%s",buf1) ;
while (getchar()!='\n'); //清空缓存区
b = my_strlen(buf1) ;
printf ("the len of s1 is %d\n\n",b);
sleep(1);
#endif
#if 1
    printf ("copy s2 to s1\n"); 
    printf ("please input a string for s1\ns1 = "); 
scanf ("%s",buf1);
while (getchar()!='\n'); //清空缓存区
    printf ("please input a string for s2\ns2 = "); 
  scanf ("%s",buf2) ;
while (getchar()!='\n'); //清空缓存区
#ifdef DEBUG
printf ("%s\n",buf1);
printf ("%s\n",buf2);
#endif
    p = my_strcpy(buf1,buf2);
printf("the new s1 is %s\n\n",p);
sleep(1);
#endif
#if 1
    printf ("conpare s1 and s2\n"); 
    printf ("please input a string for s1\ns1 = "); 
scanf ("%s",buf1);
while (getchar()!='\n'); //清空缓存区
    printf ("please input a string for s2\ns2 = "); 
  scanf ("%s",buf2) ;
while (getchar()!='\n'); //清空缓存区
p = NULL ;
    m = my_strcmp(buf1,buf2);
{
if (m==1)
printf ("s2 is before \n\n");
if (m==-1)
printf ("s1 is before \n\n");
if (m==0)
printf ("s1 = s2 \n\n");
sleep(1);
}
#endif
#if 1
    printf ("find key in s2\n"); 
    printf ("please input a string for s2\ns2 = "); 
  scanf ("%s",buf2) ;
while (getchar()!='\n'); //清空缓存区
printf("please input a small string for key\n key = ");
scanf("%s",buf3);
while (getchar()!='\n'); //清空缓存区
p = my_strstr(buf2,buf3) ;
printf ("%s\n\n",p);
sleep(1);
#endif
return 0;
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值