c的最基本上函数实现

/***************strlen.cpp************/
int Strlen(const char *s)
{
        int iLen = 0;
        while (*s!= '\0')
        {
                s++;
                iLen++;
        }
        return iLen;

}

/***************strcpy.cpp************/
//拷贝函数

char *Strcpy(char *dest, const char *src)
{
        char * temp = dest;
        while (*src != '\0')
        {
                *dest = *src;
                src++;
                dest++;
        }
        return temp;

}


char *Strncpy(char *dest, const char *src, int n)
{
        char * temp = dest;
        while ((*src != '\0')&& (n >0))
        {
                *dest = *src;
                src++;
                dest++;
                n--;
        }
        return temp;
}

/***************strcat.cpp************/
void strcat(char *s, char *t)
{
        while (*s != '/0')
        {
                s++;
        }

        while((*s++ = *t++) != '/0')
        ;

}
/***************strcmp.cpp************/

int Strcmp(const char *s1, const char *s2)
{
        while ((*s1 == *s2)&& (*s1 !='\0'))
        {
                s1++;
                s2++;
        }

        return *s1 -*s2;

}

/***************strncmp.cpp************/

int Strncmp(const char *s1, const char *s2, int n)
{
        if (n == 0)
        {
                return 0;
        }

        while ((*s1 == *s2)&& (*s1 !='\0') && ( n-1 > 0 ))
        {
                s1++;
                s2++;
                n--;
        }

        return *s1 -*s2;

}

/***************strend.cpp************/

int strend(char *s, char *t)
{
        char *pTBegin = t;

        while (*s != '/0')
        {
                s++;
        }

        while (*t != '/0')
        {
                t++;
        }
        while (*s==*t)
        {
                if (t==pTBegin)
                {
                        return 1;
                }
                s--;
                t--;
        }
        return 0;

}
/***************LTrim.cpp************/

char *LTrim(char *str)
{
        char *p = str;

        if (str==NULL)
        {
                return ;
        }
        while(*p!='/0')
        {
                if ((*p!=' ')&&(*p!='/t'))
                {
                        break;
                }
                p++;
        }
        if (p == str)
        {
                return p;
        }

        strcpy(str,p);
        return str;

}
/***************RTrim.cpp************/
char *RTrim(char *str)
{
 int i = strlen(str)- 1;
 
 while((i>=0)&&(str[i]==' '||str[i]=='/t'))
 {
  str[i] = '/0';
  i--;
 }
 return str;

}
char *RTrim(char *str)
{
        int i = strlen(str)- 1;

        while((i>=0)&&(str[i]==' '||str[i]=='/t'))
        {
                str[i] = '/0';
                i--;
        }
        return str;


}

/***************Trim.cpp************/
char *Trim( char *str )
{
 char *pStr;
 int i = 0;

 if ( str == NULL )
  return NULL;

 pStr = str;
 while ( *pStr == ' ' || *pStr == '/t' )  pStr ++;
 strcpy( str, pStr );

 i = strlen( str ) - 1;
 while( (i>=0)&&(str[i] == ' ' || str[i] == '/t') )
 {
  str[i] = '/0';
  i -- ;
 }
 return  str;
}

int main()
{
        int i = 1;
        char *p ="abcdefg";
        char *p2="abcdefh";
        char *p3="abcdefg";
        char temp[10] = {0};
        char temp2[10] = {0};
        printf("strlen(p)= %d, Strlen(p)= %d\n", strlen(p),Strlen(p));
        printf("Strcmp(p,p2)= %d\n", Strcmp(p,p2));
        printf("Strcmp(p2,p3)= %d\n", Strcmp(p2,p3));

        printf("Strncmp(p2,p3,6)= %d\n", Strncmp(p2,p3,6));
        printf("Strncmp(p2,p3,7)= %d\n", Strncmp(p2,p3,7));
        printf("Strncmp(p2,p3,3)= %d\n", Strncmp(p2,p3,3));
        printf("Strcpy(temp, p2)= %s\n", Strcpy(temp,p2));
        printf("Strncpy(temp2, p2)= %s\n", Strncpy(temp2,p2,5));
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值