Self-implemented C string. h header file string functions and several memory operation functions

Source: Internet
Author: User

 

# Include <stdio. h>

Char * strcpy (char * DEST, const char * SRC); // copy a string
Char * strcat (char * DEST, const char * SRC); // string connection, returns the Dest string
Char * strncat (char * DEST, const char * SRC, int size); // connect the first n strings of SRC to DEST and return the Dest string
Int strcmp (const char * src1, const char * src2); // string comparison return value: 1:> 0: =-1 <
Int strncmp (const char * SRC, const char * DST, int size); // compare the first n characters of DST with the return value of SRC: 1:> 0: =-1 <
Int strlen (const char * SRC); // returns the string length.
Char * strchr (const char * SRC, int ch); // returns the pointer from the CH position to the end position of the string when the string appears for the first time.
Char * strrchr (const char * SRC, int ch); // The Position of the character Ch at the last occurrence of the string, returns the pointer from the start of the character ch position to the end of the string
Char * strstr (const char * SRC, const char * sub); // the position where the string sub appears for the first time. The pointer from the string sub position to the string end position is returned.

// Memery copy operate function ================================ //
Void * memcpy (void * DEST, const void * SRC, unsigned int size); // copy the SRC content to the Dest memory and return the memory address pointed to by DeST.
Void * memset (void * DST, int CH, unsigned int size); // initialize the size in the DST memory with CH and return the memory address pointed to by DeST.
Int memcmp (const void * src1, const void * src2, unsigned int size); // compare whether the first size characters of src1 and src2 in memory are equal, 1:> 0: =-1 <(see strncmp (const char *, const char *, unsigned INT ))
Void * memchr (const void * DST, int CH, unsigned int size); // in a memory range, locate a specific ch character and return a pointer to Ch.
Void * memmove (void * DST, const void * SRC, unsigned int size); // memmove () and memcpy () the same is used to copy the first n Bytes of the memory content specified by Src to the address indicated by DeST. The difference is that when SRC and DEST refer to memory areas overlapping, memmove () can still be correctly processed, but the execution efficiency is slightly slower than memcpy. returns the pointer to DeST.

// Memery copy operate function ================================ //

Int main ()
{
// Printf ("the string Len = % d/N", strlen ("helloworld "));

// Char Dest [20];
// Printf ("the string is = % s/n", strcpy (DEST, "helloworld "));

// Char Dest [30] = "hello --";
// Printf ("after connect sub string is = % s/n", strcat (DEST, "world "));

// Char Dest [30] = "hello --";
// Printf ("after connect sub string is = % s/n", strncat (DEST, "world_programe", 4 ));
 
// Printf ("the string compare result is = % d/N", strcmp ("hello", "aello "));

// Printf ("the string compare result is = % d/N", strncmp ("hello", "hello_good", 5 ));

// Printf ("the char appear position is = % s/n", strchr ("hello-r-es-d ",'-'));

// Printf ("the char appear position is = % s/n", strrchr ("El * lo-R *-E * s-d ",'*'));
 
// Printf ("the sub string is = % s/n", strstr ("hello_world", "Lo "));

Char Dest [20];
// Printf ("the sub string is = % s/n", memcpy (DEST, "Hello .. 111", sizeof (DEST )));
Printf ("the sub string is = % s/n", memset (DEST, '0', sizeof (DEST )));

Return 0;
}

// ================================================ ================================== //

Int strlen (const char * SRC)
{
Const char * P = SRC;
While (* P ++! = '/0 ');
Return p-src-1;
}

// ================================================ ================================== //
Char * strcpy (char * DEST, const char * SRC)
{
If (DEST & SRC)
{
Int I = 0;
While (* (DEST + I) = * (SRC + I ))! = '/0') I ++;
* (DEST + I) = '/0 ';
// Return DEST;
}
Return DEST;
}

// ================================================ ================================== //
Char * strcat (char * DEST, const char * SRC)
{
If (DEST & SRC)
{
Int Len = strlen (DEST );
// Printf ("Len = % d/N", Len );
Int I = 0;
While (* (DEST + Len + I) = * (SRC + I ))! = '/0') I ++;
* (DEST + Len + I) = '/0 ';
}
Return DEST;
}

// ================================================ ================================== //
Char * strncat (char * DEST, const char * SRC, int size)
{
If (DEST & SRC)
{
Int Len = strlen (DEST );
Int I = 0;
While (I <size) & (* (DEST + Len + I) = * (SRC + I ))! = '/0') I ++;
}
Return DEST;
}

// ================================================ ================================== //
Int strcmp (const char * src1, const char * src2)
{
Int equal;
Int I = 0;
While (! (Equal = * (unsigned char *) (src1 + I)-* (unsigned char *) (src2 + I) & (* (src1 + I )) & (* (src2 + I )))
I ++;
If (equal <0) Return-1;
Else if (equal> 0) return 1;
Else return 0;
}

// ==================================Compare the size strings before the two strings to see if they are equal ================ ====================== //
Int strncmp (const char * src1, const char * src2, int size)
{
Int equal;
Int I = 0;
While (I <size )&&! (Equal = * (unsigned char *) (src1 + I)-* (unsigned char *) (src2 + I) & (* (src1 + I )) & (* (src2 + I )))
I ++;
If (equal <0) Return-1;
Else if (equal> 0) return 1;
Else return 0;
}

// ================================================ ================================== //
Char * strchr (const char * SRC, int ch)
{
Int I = 0;
While (* (SRC + I) & (* (SRC + I )! = CH) I ++;
Return (char *) (SRC + I );
}

// ================================================ ================================== //
Char * strrchr (const char * SRC, int ch)
{
Int Len = strlen (SRC );
Int I = 0;
While (I <Len & (* (SRC + len-I )! = CH) I ++;
Return (char *) (SRC + (LEN-I ));
}

// ================================================ ================================== //
Char * strstr (const char * SRC, const char * sub)
{
If (SRC & sub)
{
Int sublen = strlen (sub );
Int srclen = strlen (SRC );
Int nomatch = 1;
Int FDS = srclen-sublen + 1;
Int I = 0;
If (FDS> 0) // find counts in the string
While (nomatch = strncmp (SRC + I, sub, sublen) & FDS --) // pushes the current SRC pointer back until it finds the same as the sub pointer.
I ++;
If (nomatch)
Return 0;
Else
Return (char *) (SRC + I );
}
Return 0;
}

// ================================== Memory operate ====================== ====================================== //
// ================================== Memory operate ====================== ====================================== //
Void * memcpy (void * DEST, const void * SRC, unsigned int size)
{
If (DEST & SRC & size> 0)
{
Int I = 0;
Unsigned char * P = (unsigned char *) DEST;
Unsigned char * q = (unsigned char *) SRC;
While (I <size) & (* (p + I) = * (q + I )))
I ++;
Return DEST;
}
Return 0;
}

// ================================================ ================================== //
Void * memset (void * DST, int CH, unsigned int size)
{
Int I = 0;
Unsigned char * P = (unsigned char *) DST;
While (I <size) & (* (p + I) = CH ))
I ++;
Return DST;
}

// ================================================ ================================== //
Int memcmp (const void * src1, const void * src2, unsigned int size)
{

}

// ================================================ ================================== //
Void * memchr (const void * DST, int CH, unsigned int size)
{

}

Void * memmove (void * DST, const void * SRC, unsigned int count)
{
Void * ret = DST;
If (DST <= SRC | (char *) DST> = (char *) SRC + count ))
{
While (count --)
{
* (Char *) DST = * (char *) SRC;
DST = (char *) DST + 1;
Src = (char *) SRC + 1;
}
}
Else
{
DST = (char *) DST + count-1;
Src = (char *) SRC + count-1;
While (count --)
{
* (Char *) DST = * (char *) SRC;
DST = (char *) DST-1;
Src = (char *) Src-1;
}
}

Return (RET );

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.