c 문자열 처리 함수 구현
#include <stdio.h>
//copying
void * m_memcpy ( void * destination, const void * source, size_t num );
void * m_memmove ( void * destination, const void * source, size_t num );
void * m_memccpy ( void * destination, const void * source, int c, size_t num ); //not standard
char * m_strcpy ( char * destination, const char * source );
char * m_strncpy ( char * destination, const char * source, size_t num );
//Concatenation
char * m_strcat ( char * destination, const char * source );
char * m_strncat ( char * destination, char * source, size_t num );
//Comparison
int m_memcmp ( const void * ptr1, const void * ptr2, size_t num );
int m_strcmp ( const char * str1, const char * str2 );
int m_strcoll ( const char * str1, const char * str2 ); //similar to m_strcmp, not implemented
int m_strncmp ( const char * str1, const char * str2, size_t num );
size_t m_strxfrm ( char * destination, const char * source, size_t num ); //not implemented
//Searching
void * m_memchr ( void * ptr, int value, size_t num );
char * m_strchr ( char * str, int character );
size_t m_strcspn ( const char * str1, const char * str2 );
char * m_strpbrk ( char * str1, const char * str2 );
char * m_strrchr ( char * str, int character );
size_t m_strspn ( const char * str1, const char * str2 );
char * m_strstr ( char * str1, const char * str2 );
char * m_strtok ( char * str, const char * delimiters );
//Others
void * m_memset ( void * ptr, int value, size_t num );
size_t m_strlen ( const char * str );
char * strerror ( int errnum ); //not implemented
실현 함수
#include <stdio.h>
#include <assert.h>
#include <string.h>
#include "m_string.h"
#define MAX_LEN 1000
/*
* memcpy()、 memmove() memccpy()
* 。
* memmove() memcpy() src n dest 。
* , src dest ,memmove() , memcpy() 。
* , 。
*/
void * m_memmove ( void * destination, const void * source, size_t num )
{
assert(NULL!=destination && NULL!=source);
char *des = (char *)destination;
const char *src = (const char *)source;
if(des < src)
{
while(num--)
{
*des++ = *src++;
}
}
else
{
des += num;
src += num;
while(num--)
{
//*des-- = *src--;
*--des = *--src;
}
}
return destination;
}
/*
* : src count dest , c 。
* : c , NULL, , dest 。
*/
void * m_memccpy ( void * destination, const void * source, int c, size_t num )
{
assert(NULL!=destination && NULL!=source);
char *des = (char *)destination;
const char *src = (const char *)source;
while(num--)
{
if(*src == (char)c)
{
return des+1;
}
*des++ = *src++;
}
return NULL;
}
//
char * m_strcpy ( char * destination, const char * source )
{
assert(NULL!=destination && NULL!=source);
char *des = destination;
while(*source)
{
*des++ = *source++;
}
*des = 0;
return destination;
}
//
char * m_strncpy ( char * destination, const char * source, size_t num )
{
assert(NULL!=destination && NULL!=source);
char *des = destination;
while(num--)
{
*des++ = *source;
if(*source)
{
++source;
}
}
return destination;
}
//
char * m_strcat ( char * destination, const char * source )
{
assert(NULL!=destination && NULL!=source);
char *des = destination;
while(*des) ++des;
while(*source)
{
*des++ = *source++;
}
*des = 0;
return destination;
}
//
char * m_strncat ( char * destination, char * source, size_t num )
{
assert(NULL!=destination && NULL!=source);
char *des = destination;
while(*des) ++des;
while(num--)
{
*des++ = *source;
if(*source)
{
++source;
}
else
{
break;
}
}
*des = 0;
return destination;
}
//
int m_memcmp ( const void * ptr1, const void * ptr2, size_t num )
{
assert(NULL!=ptr1 && NULL!=ptr2);
const char *p1 = (const char *)ptr1;
const char *p2 = (const char *)ptr2;
int res = 0;
while(num-- && !res)
{
res = *p1++ - *p2++;
}
return res;
}
/*
* This function starts comparing the first character of each string.
* If they are equal to each other, it continues with the following pairs
* until the characters differ or until a terminanting null-character is reached.
*/
int m_strcmp ( const char * str1, const char * str2 )
{
assert(NULL!=str1 && NULL!=str2);
while (*str1 && *str2 && *str1 == *str2)
{
++ str1;
++ str2;
}
return (*str1 - *str2); //ingenious, '\0' is also compared
}
//
int m_strncmp ( const char * str1, const char * str2, size_t num )
{
assert(NULL!=str1 && NULL!=str2);
int res = 0;
while (num-- && !res)
{
res = *str1 - *str2;
if(!*str1 || !*str2)
{
return res;
}
++ str1;
++ str2;
}
return res;
}
void * m_memchr ( void * ptr, int value, size_t num )
{
assert(NULL != ptr);
char *pos = (char *)ptr;
while(num--)
{
if(*pos == (char)value)
{
return (void *)pos;
}
++ pos;
}
return NULL;
}
//
char * m_strchr ( char * str, int character )
{
assert(NULL != str);
while(*str)
{
if(*str == (char)character)
{
return str;
}
++ str;
}
return (*str == (char)character) ? str : NULL;
}
//
size_t m_strcspn ( const char * str1, const char * str2 )
{
assert(NULL!=str1 && NULL!=str2);
const char *pos = str1;
const char *keys;
while(*pos)
{
keys = str2;
while(*keys)
{
if(*pos == *keys++)
{
return pos - str1;
}
}
++ pos;
}
return pos - str1;
}
//
char * m_strpbrk ( char * str1, const char * str2 )
{
assert(NULL!=str1 && NULL!=str2);
char *pos = str1;
const char *keys;
while(*pos)
{
keys = str2;
while(*keys)
{
if(*pos == *keys++)
{
return pos;
}
}
++ pos;
}
return NULL;
}
//
char * m_strrchr ( char * str, int character )
{
assert(NULL != str);
char *pos = str;
while(*pos) ++pos;
-- pos;
while(pos >= str)
{
if(*pos == (char)character)
{
return pos;
}
-- pos;
}
return NULL;
}
//
size_t m_strspn ( const char * str1, const char * str2 )
{
assert(NULL!=str1 && NULL!=str2);
const char *pos = str1;
const char *keys;
while(*str1)
{
keys = str2;
while(*keys)
{
if(*pos == *keys)
{
break;
}
++ keys;
}
if(*keys == '\0') break;
++ pos;
}
return pos-str1;
}
//
char * m_strstr ( char * str1, const char * str2 )
{
assert(NULL!=str1 && NULL!=str2);
size_t len2 = m_strlen(str2);
for(; *str1; ++str1)
{
if(!m_strncmp(str1, str2, len2))
{
return str1;
}
}
return 0;
}
//
char * m_strtok ( char * str, const char * delimiters )
{
assert(NULL!=str && NULL!=delimiters);
static char *start;
char *end;
if(str)
{
start = str;
}
while(*start)
{
if(m_IsDelimiter(*start, delimiters))
{
++ start;
}
else
{
break;
}
}
if(*start)
{
end = start + 1;
while(*end)
{
if(m_IsDelimiter(*end, delimiters))
{
*end = '\0';
char *pos = start;
start = end + 1;
return pos;
}
++ end;
}
if('\0' == *end)
{
char *pos = start;
start = end;
return pos;
}
}
return 0;
}
int m_IsDelimiter(const char c, const char * delimiters)
{
assert(NULL != delimiters);
while(*delimiters)
{
if(c == *delimiters++)
{
return 1;
}
}
return 0;
}
//
void * m_memset ( void * ptr, int value, size_t num )
{
assert(NULL != ptr);
char *p = (char *)ptr;
while(num--)
{
*p++ = (char)value;
}
return ptr;
}
//
size_t m_strlen ( const char * str )
{
assert(NULL != str);
const char *pos = str;
while(*pos) ++pos;
return pos - str;
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Docker를 사용한 React 및 .NET Core 6.0 샘플 프로젝트 - 1부이 기사에서는 Entity Framework Core Code First 접근 방식을 사용하는 ASP.NET Core 6.0 WEP API의 CRUD(만들기, 읽기, 업데이트 및 삭제) 작업에 대해 설명합니다. 웹 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.