24 December 2013

copy

/* copy the string from src to dest, build in string.h */
char *strcpy(char *dest, const char *src)
{
    char *s = dest;
    while(*s++ = *src++);
    return dest;
}

concat

/* concat src with dest */
char *strcat(char *dest, const char *src)
{
    char *s = dest;
    while(*s) s++;
    while(*s++ = *src++);
    return dest;
}

strncat

/* 复制 src 中前 n 个字符到 dest 末尾 */
char *strncat(char *dest, const char *src, int n)
{
	char *d;

	d = dest;
	while (*d) d++;
	while (n-- && (*d++ = *src++));
	if (n < 0)
		*d = '\0';
	return dest;
}

compare

/* version 1: compare s1 with s2, return s1 - s2 */
int strcmp1(const char *s1, const char *s2)
{
    for(; *s1 == *s2; ++s1, ++s2)
    {
        if(*s1 == '\0')
            return 0;
    }
    return *s1 - *s2;
}
/* version 2: compare s1 with s2, return s1 - s2 */
int strcmp2(const char *s1, const char *s2)
{
    for(; *s1 == *s2 && *s1; ++s1, ++s2);

    return *s1 - *s2;
}
/* version 3: compare s1 with s2, return 1 or -1 or 0 */
int strcmp3(const char *s1, const char *s2)
{
    int ret = 0;
    while(!(ret = *(unsigned char *)s1 - * (unsigned char *)s2) && *s2) ++s1, ++s2;

    if(ret < 0)
    {
        ret = -1;
    }
    else if(ret > 0)
    {
        ret = 1;
    }
    return ret;
}

reverse

/* version 1: compare with the max exchange num, and use len - i */
void Reverse1(char *str)
{
    int len = strlen(str);
    int count = len / 2;
    int i;

    --len;
    for( i = 0; i < count; i++)
    {
        char temp = str[i];
        str[i] = str[len - i];
        str[len - i] = temp;
    }
}
/* version 2: compare with address of pointer and exchange value wtih pointer */
void Reverse2(char *str)
{
    int len;
    char *p, *q;

    p = q = str;
    len = strlen(str);
    for(q += len - 1; p < q; ++p, --q)
    {
        char temp = *p;
        *p = *q;
        *q = temp;
    }
}
/* version 3: compare and exchange value with array index */
void Reverse3(char *str)
{
    int c, i, j;

    for(i = 0, j = strlen(str) - 1; i < j; ++i, --j)
    {
        c = str[i];
        str[i] = str[j];
        str[j] = c;
    }
}
/* version 4: base on version 3, use the comma expression */
void Reverse4(char *str)
{
    int c, i, j;

    for(i = 0, j = strlen(str) - 1; i < j; ++i, --j)
        c = str[i], str[i] = str[j], str[j] = c;
}
/* version 5: this method in string.h */
char *strrev(char *s)
{
    char *start = s;
    char *left = s;
    char ch;

    while(*s++);
    s -= 2;

    while(left < s)
    {
        ch = *left;
        *left++ = *s;
        *s-- = ch;
    }
    return start;
}

remove key

I have asked a question about how to optimize this remove() method on stackoverflow -> here

/* version 1: remove character key by two loop */
char *Remove(char *str, const char key)
{
	int len = strlen(str);
	for (int i = 0; i < len; i++)
	if (str[i] == key)
	{
		for (int j = i--; j <= len; j++)
			str[j] = str[j + 1];
	}
	return str;
}
/* version 2: remove character key by one loop */
char *Remove2(char *str, const char key)
{
	char *p1, *p2;

	p1 = p2 = str;
	while (*p2)
	{
		if (*p1 == *p2 && *p1 != key)
			p1++;
		else if (*p2 != key)
			*p1++ = *p2;
		p2++;
	}
	*p1 = *p2;
	return str;
}
/* version 3: */
char *Remove3(char *str, const char key)
{
	char *p1, *p2;

	p1 = p2 = str;
	do
	{
		while (*p2 == key) p2++;
		*p1++ = *p2++;
	} while (p2[-1]);
	return str;
}
/* version 4 */
char *Remove4(char *str, char key)
{
	char *dst = str;
	for (char *src = str; *src; src++)
	{
		if (*src != key)
			*dst++ = *src;
	}
	*dst = '\0';
	return str;  // Or src, but knowing the end is more useful!
}

remove repeat element

元素必须非递减有序

void DeleteRepeateElement(SeqList *L)
{
    int i, j;

    for(i = 0, j = 1; j <= L->last; j++)
    {
        if(L->A[i] != L->A[j])
            L->A[++i] = L->A[j];
    }
    L->last = i;
}