自主编程实现strlen,strcpy,strcmp,strcnpy,strcat,strstr,strchr库函数

        为了对strlen,strcpy,strcmp,strcnpy,strcat,strstr,strchr这几个库函数有深刻的理解和认识,所以自己就

编写了一些程序函数来实现这些库函数的功能,下面是具体的程序代码:



      (1)strlen:计算给定字符串的(unsigned int型)长度,不包括'\0'在内

#include <stdio.h>
#define N 100

int my_strlen(char temp[])
{
    int nNum = 0 ;
    while(temp[nNum] != '\0')  
    {
        nNum++ ;
    }

    return nNum ;
}

int main()
{
    char temp[N];
    scanf("%s",temp);
    int nLength = my_strlen(temp) ;
    printf("%d\n",nLength) ;

    return 0 ;
}



      (2)strcpy:字符串拷贝

#include <stdio.h>
#define N 100

char * my_strcpy(char*temp1,const char*temp2)
{
   while(*temp2 != '\0')
   {
      *temp1 = *temp2 ;
       temp1++;
       temp2++;
   }
   *temp1 = '\0' ;
   return temp1 ;
}

int main()
{
   char temp1[N] ;
   char temp2[N] ;
   scanf("%s",temp1) ;
   scanf("%s",temp2) ;
   my_strcpy(temp1,temp2);
   printf("%s\n",temp1);

   return 0 ;
}



      (3)strcmp:比较两个字符串,设这两个字符串为str1,str2,若str1==str2,则返回零;若str1<str2,则返回负数;若str1>str2,则返回正数

#include <stdio.h>
#define N 100

int my_strcmp(char * temp1 ,char *temp2)
{
    int i ;
    for(i=0;i<N;i++)
    {
        if(*(temp1+i) == *(temp2+i))
	{
	    if(*(temp1+i) == '\0')
	    {
	        break ;
	    }
	    continue ;
	}
	return (*(temp1+i)>*(temp2+i)?1:-1);
    }
    return 0 ;
}

int main()
{
   char temp1[N] ;
   char temp2[N] ;
   scanf("%s",temp1) ;
   scanf("%s",temp2) ;
   int nResult = my_strcmp(temp1,temp2);
   printf("%d",nResult) ;

   return 0 ;
}



      (4)strcnpy:把scr所指向的字符串中以scr地址开始的前n个字节复制到dest所指的数组中,并返回dest

#include <stdio.h>
#define N 100

char * my_strcnpy(char *temp1,char *temp2,int nLocation)
{
     int i,j;
     for(i=0;(i<nLocation)&&(*(temp2+i)!='\0');i++)
     {
         *(temp1+i) = *(temp2+i) ;
         j=i ;
     }
     *(temp1+j+1) = '\0' ; 
     return temp1 ;
}

int main()
{	
   char temp1[N] ;
   char temp2[N] ;
   int  nLocation ;    //位置
   scanf("%s",temp1) ;
   scanf("%s",temp2) ;
   scanf("%d",&nLocation) ;
 
   my_strcnpy(temp1,temp2,nLocation);

   printf("%s\n",temp1);

   return 0 ;
}



      (5)strcat:将两个char类型连接

#include <stdio.h>
#define N 100
char * my_strcat(char * temp1,char *temp2)
{
   int i=0,j ;
   while(*(temp1+i)!='\0')
   {
       i++ ;
   }
   for(j=0;*(temp2+j)!='\0';j++,i++)
   {
       *(temp1+i) = *(temp2+j) ;
   }
   *(temp1+i)='\0' ;

   return temp1 ;
}

int main()
{
    char temp1[N] ;
    char temp2[N] ;
    scanf("%s",temp1) ;
    scanf("%s",temp2) ;
    my_strcat(temp1,temp2) ;

    printf("%s\n",temp1);
    return 0 ;
}



      (6)strstr:判断字符串str2是否是str1的子串。如果是,则该函数返回str2在str1中首次出现的地址;否则,返回NULL

#include <stdio.h>
#define N 100

const char *my_strstr(char *temp1,char *temp2)
{
    // assert(NULL != temp1 && NULL != temp2) ;
    while(*temp1 != '\0')
    {
       const char * t1 = temp1 ;
       const char * t2 = temp2 ;
       const char * current = NULL ;
       if(*t1 == *t2)
       {
          current = t1 ;
          while(*t1!='\0'&&*t2!='\0'&&*t1==*t2)
	  {
	       t1++ ;
	       t2++ ;	       
	  }
	  if(*t2 == '\0')
	  {
	      return current ;
	  }
       }
       temp1++ ;
    }
    return NULL ;
}

int main()
{
    char temp1[N] ;
    char temp2[N] ;
    scanf("%s",temp1) ;
    scanf("%s",temp2) ;

    printf("%s\n",my_strstr(temp1,temp2));
    return 0 ;
}
	



      (7)strchr::用来查找某字符在字符串中首次出现的位置

#include <stdio.h>
#define N 100 

char * my_strchr(char *temp,char c)
{
    //assert(temp != NULL);
    char * location ;
    location = temp ;
    while(*location != '\0')
    {
        if(*location == c)
	{
	    return location ;
	}
	location++ ;
    }
    return NULL ;
}

int main()
{
    char temp[N] =" haohaoxuexi";
    char character = 'a';
    printf("%s\n",my_strchr(temp,character));
    
    return 0 ;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值