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
", strlen(p),Strlen(p)); printf("Strcmp(p,p2)= %d
", Strcmp(p,p2)); printf("Strcmp(p2,p3)= %d
", Strcmp(p2,p3)); printf("Strncmp(p2,p3,6)= %d
", Strncmp(p2,p3,6)); printf("Strncmp(p2,p3,7)= %d
", Strncmp(p2,p3,7)); printf("Strncmp(p2,p3,3)= %d
", Strncmp(p2,p3,3)); printf("Strcpy(temp, p2)= %s
", Strcpy(temp,p2)); printf("Strncpy(temp2, p2)= %s
", Strncpy(temp2,p2,5)); }

좋은 웹페이지 즐겨찾기