⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 str.cpp

📁 常见算法的代码包
💻 CPP
字号:
/********************************************************************
*strcat Purpose:
*       Concatenates src onto the end of dest.  Assumes enough
*       space in dest.
*********************************************************************/

char * strcat (char *dst, const char *src)
{
        char * cp = dst;
        while( *cp )
                cp++;                   /* find end of dst */
        while( *cp++ = *src++ ) ;       /* Copy src to end of dst */
        return( dst );                  /* return dst */
}


/********************************************************************
*strncat Purpose:
*       Appends at most count characters of the string back onto the
*       end of front, and ALWAYS terminates with a null character.
*       If count is greater than the length of back, the length of back
*       is used instead.  (Unlike strncpy, this routine does not pad out
*       to count characters).
*********************************************************************/

char * strncat (char *front, const char *back, size_t count)
{
        char *start = front;
        while (*front)
        {
		front++;
	}
        while (count--)
                if (!(*front++ = *back++))
                        return(start);
        *front = '\0';
        return(start);
}


/********************************************************************
*strchr Purpose:
*       Searches a string for a given character, which may be the
*       null character '\0'.
*********************************************************************/

char * strchr (const char *string, int ch)
{
        while (*string && *string != (char)ch)
                string++;
        if (*string == (char)ch)
                return((char *)string);
        return(NULL);
}


/********************************************************************
*strcmp Purpose:
*       STRCMP compares two strings and returns an integer
*       to indicate whether the first is less than the second, the two are
*       equal, or whether the first is greater than the second.
*********************************************************************/

int strcmp (const char *src, const char *dst)
{
        int ret = 0 ;
        while( ! (ret = *(unsigned char *)src - *(unsigned char *)dst) && *dst)
                ++src, ++dst;

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

        return( ret );
}


/********************************************************************
*strlen Purpose:
*       Finds the length in bytes of the given string, not including
*       the final null character.
*********************************************************************/

unsigned int strlen (const char *str)
{
        const char *eos = str;
        while( *eos )
		{
			eos++;
		}
        return ( eos - str );
}


/********************************************************************
*strncpy Purpose:
*       defines strncpy() - copy at most n characters of string
*********************************************************************/

char *  strncpy (char *dest, const char *source, size_t count)
{
        char *start = dest;
        while (count && (*dest++ = *source++))    /* copy string */
                count--;
        if (count)                              /* pad out with zeroes */
                while (--count)
                        *dest++ = '\0';
        return (start);
}

char * strcpy(char *dest, const char *source)
{
	char *start = dest;
	assert(dest != NULL && source != NULL);
	while( (*dest++ = *source++) != '\0')
		NULL;
	return start;
}

/********************************************************************
*strrev Purpose:
*       Reverses the order of characters in the string.  The terminating
*       null character remains in place.
*********************************************************************/

void strrev(char *str)
{
	char *start = str;
	char c;
	while(*str)
		++str;
	--str;
	while(start < str)
	{
		c = *start;
		*start++ = *str;
		*str-- = c;
	}
}


/********************************************************************
*strset Purpose:
*       defines _strset() - sets all of the characters in a string (except
*       the '\0') equal to a given character.
*********************************************************************/

char * strset (char *string, int val)
{
        char *start = string;
        while (*string)
                *string++ = (char)val;
        return(start);
}


/********************************************************************
*strstr Purpose:
*       finds the first occurrence of string2 in string1
*
*Entry:
*       char *string1 - string to search in
*       char *string2 - string to search for
*
*Exit:
*       returns a pointer to the first occurrence of string2 in
*       string1, or NULL if string2 does not occur in string1
*********************************************************************/

char *  strstr (const char *str1, const char *str2)
{
        char *cp = (char *) str1;
        char *s1, *s2;

        if ( !*str2 )
            return((char *)str1);

        while (*cp)
        {
                s1 = cp;
                s2 = (char *) str2;

                while ( *s1 && *s2 && !(*s1-*s2) )
                        s1++, s2++;

                if (!*s2)
                        return(cp);

                cp++;
        }

        return(NULL);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -