[면접 중] C 언어 구현 문자열 함수

9133 단어
C 언어 문자열 함수 원본 요약
2008년 05월 18일 일요일 16:54
 #include

 char * ___strtok = NULL;

 char * strcpy(char * dest,const char *src)
 {
          char *tmp = dest;

          while ((*dest++ = *src++) != '\0')
  ;
          return tmp;
 }

 char * strncpy(char * dest,const char *src,size_t count)
 {
          char *tmp = dest;

          while (count-- && (*dest++ = *src++) != '\0')
                  ;

          return tmp;
 }

 char * strcat(char * dest, const char * src)
 {
          char *tmp = dest;

          while (*dest)
                  dest++;
          while ((*dest++ = *src++) != '\0')
                  ;

          return tmp;
 }

 char * strncat(char *dest, const char *src, size_t count)
 {
          char *tmp = dest;

          if (count) {
                  while (*dest)
                          dest++;
                  while ((*dest++ = *src++)) {
                          if (--count == 0)
                                  break;
                  }
          }

          return tmp;
 }

 int strcmp(const char * cs,const char * ct)
 {
          register signed char __res;

          while (1) {
                  if ((__res = *cs - *ct++) != 0 || !*cs++)
                          break;
          }

         return __res;
 }

 int strncmp(const char * cs,const char * ct,size_t count)
 {
          register signed char __res = 0;

          while (count) {
                  if ((__res = *cs - *ct++) != 0 || !*cs++)
                          break;
                  count--;
          }

          return __res;
 }

 char * strchr(const char * s,char c)
 {
          for(; *s != c; ++s)
                  if (*s == '\0')
                          return NULL;
          return (char *) s;
 }

 size_t strlen(const char * s)
 {
          const char *sc;

          for (sc = s; *sc != '\0'; ++sc)
                  ;
          return sc - s;
 }

 size_t strnlen(const char * s, size_t count)
 {
          const char *sc;

          for (sc = s; *sc != '\0' && count--; ++sc)
                  ;
          return sc - s;
 }

 size_t strspn(const char *s, const char *accept)
 {
          const char *p;
          const char *a;
          size_t count = 0;

          for (p = s; *p != '\0'; ++p) {
                  for (a = accept; *a != '\0'; ++a) {
                          if (*p == *a)
                                  break;
                  }
                  if (*a == '\0')
                          return count;
                  ++count;
          }

          return count;
 }

 char * strpbrk(const char * cs,const char * ct)
 {
          const char *sc1,*sc2;

          for( sc1 = cs; *sc1 != '\0'; ++sc1) {
                  for( sc2 = ct; *sc2 != '\0'; ++sc2) {
                          if (*sc1 == *sc2)
                                  return (char *) sc1;
                  }
          }
          return NULL;
 }

 char * strtok(char * s,const char * ct)
 {
          char *sbegin, *send;

          sbegin   = s ? s : ___strtok;
          if (!sbegin) {
                  return NULL;
          }
          sbegin += strspn(sbegin,ct);
          if (*sbegin == '\0') {
                  ___strtok = NULL;
                  return( NULL );
          }
          send = strpbrk( sbegin, ct);
          if (send && *send != '\0')
                  *send++ = '\0';
          ___strtok = send;
          return (sbegin);
 }

 void * memset(void * s,char c,size_t count)
 {
          char *xs = (char *) s;

          while (count--)
                  *xs++ = c;

          return s;
 }

 char * bcopy(const char * src, char * dest, int count)
 {
          char *tmp = dest;

          while (count--)
                  *tmp++ = *src++;

          return dest;
 }

 void * memcpy(void * dest,const void *src,size_t count)
 {
          char *tmp = (char *) dest, *s = (char *) src;

          while (count--)
                  *tmp++ = *s++;

          return dest;
 }

 void * memmove(void * dest,const void *src,size_t count)
 {
          char *tmp, *s;

          if (dest <= src) {
                  tmp = (char *) dest;
                  s = (char *) src;
                  while (count--)
                          *tmp++ = *s++;
                  }
          else {
                  tmp = (char *) dest + count;
                  s = (char *) src + count;
                  while (count--)
                          *--tmp = *--s;
                  }

          return dest;
 }

 int memcmp(const void * cs,const void * ct,size_t count)
 {
          const unsigned char *su1, *su2;
          signed char res = 0;

          for( su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
                  if ((res = *su1 - *su2) != 0)
                          break;
          return res;
 }


 void * memscan(void * addr, unsigned char c, size_t size)
 {
          unsigned char * p = (unsigned char *) addr;

          while (size) {
                  if (*p == c)
                          return (void *) p;
                  p++;
                  size--;
          }
          return (void *) p;
 }

1.strlen(), 문자열 길이 계산
int strlen (const char *s)
{
const char *p;
for (p = s; *p; ++p);
return p - s;
}

2. strcpy(), 
문자열 복사
.
char *stpcpy (char *dst, const char *src)
{
while ((*dst++ = *src++) != '\0');
return dst - 1;
}

3. strcat (), 문자열의 연결.
char *strcat (char *dst, const char *src)
{
char *ret = dst;
while (*dst++ != '\0');
--dst;
while ((*dst++ = *src++) != '\0');
return ret;
}

4. streql(), 
두 문자열이 같은지 아닌지를 판단하다
.
int streql(char *str1,char *str2)
{
while((*str1==*str2)&&(*str1))
{
str1++;
str2++;
}
return((*str1==NULL)&&(*str2==NULL));
}
5. strchr(),            .
char *strchr (const char *s, int c)
{
for (; *s != (char) c; ++s)
if (*s == '\0')
return NULL;
return (char *) s;
}
6. chrcnt(), 문자열에 나타나는 문자의 수를 계산합니다.
int chrcnt(const char *string,int letter)
{
int count=0;
while(*string)
{
if(*string==letter)
count++;
string++;
}
return count;
}
7.strcmp(), 두 문자열이 같은지 판단합니다.
int strcmp (const char *s1, const char *s2)
{
int ret;
while ((ret = *(unsigned char *) s1 - *(unsigned char *) s2++) == 0
&& *s1++);
return ret;
}
int my_atoi(const char *str){   int result;                //          int signal = 1;            //     ,         //              
if((*str>='0'&&*str<='9')||*str=='-'||*str=='+'){     //   -,    -1,         //   +,    (          )   
if(*str=='-'||*str=='+'){     if(*str=='-')   signal = -1;    str++;   }    }   
else return 0;//     +,-   ,    0   //      ,             
 while(*str>='0'&&*str<='9')   result = result*10+(*str++ -'0');   return signal*result;   }
double atof(char s[])        {            double val, power;            int i, sign;               
  for (i = 0; isspace(s[i]); i++)                 ;            sign = (s[i] == '-') ? -1 : 1;           
 if (s[i] == '+' || s[i] == '-')                i++;           
 for (val = 0.0; isdigit(s[i]); i++)                val = 10.0 * val + (s[i] - '0');            
if (s[i] == '.')                i++;           
 for (power = 1.0; isdigit(s[i]); i++) {                val = 10.0 * val + (s[i] -‘0');                 power *= 10;            }          
  return sign * val / power;        }
long __cdecl atol(const char* nptr)
{
         int c;
         int sign;
         long total;
         while(isspace((int)(unsigned char)*nptr))
        {
                 ++nptr;
        }

       c = (int)(unsigned char)*nptr++;
       sign = c;
       if(c == "-" || c == "+")
       {
               c = (int)(unsigned char)*nptr++;
       }
       total = 0;
       while(isdigit(c))
       {
                  total = 10*total + ('c'-0);
                  c = (int)(unsigned char)*nptr++;
       }
       return (sign == '-') ? -total : total;
}
 
assert() 함수를 충분히 이용하여 먼저 판단한다.

좋은 웹페이지 즐겨찾기