String.h C Source Code implementation

Source: Internet
Author: User

int isspace (char c) {char comp[] = {"', ' \ t ', ' \ R ', ' \ n ', ' \v ', ' \f '};const int len = 6;for (int i=0; i<len; ++i) {if (c = = Comp[i]) return 1;} return 0;} Char ToLower (char c) {if (c >= ' A ' && C <= ' Z ') return (c + +); return C;} Finds the position of the first occurrence of C in the string S. Find: Returns the pointer position; Not found: Returns NULLCHAR * STRCHR (const char * s, int c) {for (; *s! = (char) c; ++s) {if (*s = = ') ') return NULL;//If s has reached the tail, Find, return Null}return (char *) s; Returns C at the pointer position of S}//in the string s from the end to find the character C, found returns the string pointer position, not found return Nullchar * STRRCHR (const char * s, int c) {const char * p = s + St Rlen (s); P points to the end of S do {if (*p = = (char) c) return (char *) p;//} while (--p >= s); return NULL;} Finds the character C within the specified length in the string s. char * STRNCHR (const char * s, size_t count, int c) {for (; count--&& *s! = '); ++s) {if (*s = = (char) c) RET Urn (char *) s;} return NULL;}  Search for a string S2 in another string S1 the first occurrence of char * STRSTR (const char * s1, const char * s2) {size_t L1, l2;l2 = strlen (s2); if (!l2 ) return (char *) S1;L1 = strlen (S1); while (L1 >= L2) {l1--;if (!memcmp (S1, S2, L2)) return (char *) s1;s1++;} return NULL;} Finds the number of characters from the beginning of the string s that conform to the accept character and returns the string s beginning with the string accept. If n is returned, it represents the character in the string accept that has n characters at the beginning of the string s. Char *str= "Linux is first developed for 386/486-based PCs."; /printf ("%d\n", strspn (str, "Linux")); The output is 5//printf ("%d\n", strspn (str, "/-")); The output is 0//printf ("%d\n", strspn (str, "1234567890")); The output is 0size_t strspn (const char * s, const char * accept) {const char * p;const char * a;size_t count = 0;for (p = s; * P! = ' + '; ++p) {for (a = accept; *a! = '); ++a)//*p one-by-one with the string alignment allowed by accept. {if (*p = = *a)//As long as the accept, jump out of the accept loop, count+1break;} if (*a = = ')//If the accept is at the end, the first character position that does not conform to accept is present. return count;++count;} return count;}  Find the reject character from the beginning of the string s, return string s at the beginning of the string reject the number of characters//if return n, which means that the string s starts with n characters without characters within the string reject.//char *str = "Linux was first developed for 386/486-based PCs. ";//printf ("%d\n ", strcspn (str," ")); Output 5, only the "" appears, so return the "Linux" Length//printf ("%d\n", strCSPN (str, "/-")); Output 33; Calculated to appear "/" or "-", so return to "6" of length//printf ("%d\n", strcspn (str, "1234567890")); Output 30;  Evaluates to a number character, so returns the length of "3" before it appears size_t strcspn (const char * s, const char * reject) {const char * p;const char * r;size_t count = 0;for (p = s; *p = ' *r '; ++p) {for (R = Reject; ++r! = '); *p) {if (*r = =)//If *p equals *r, returns the length return count ;} ++count; 2 are unequal, count+1}return count;} In the string S1, the S2 string is examined, and when the S2 character is in S1, the test is stopped and the character position is returned//char *s = "12,3a,45"//char *ptr = STRPBRK (S, ","); PTR =, 3a,45char * STRPBRK (const char * s1, const char * s2) {const char * SC1, * sc2;for (SC1 = S1; *sc1! = '); + + SC1) {for (SC2 = s2; ++sc2) {if (*SC1 = *SC2)//If the S1 character return (char *) S2 is found in SC1;//returns the character position}} return NULL; Not found returns Null}char * STRSEP (char **s, const char *ct) {char * sbegin = *s;char * END;IF (*sbegin = = NULL) return null;e nd = STRPBRK (sbegin, CT); Find the position of CT first outlet in Sbegin. if (end)//found *end++ = ' + '; Add terminator ' *s ' = end; s points to a new openStart position return sbegin;} Skip whitespace character char * skip_spaces (const char * str) {while (Isspace (*STR)) ++str;return (char *) str;} Skip end-to-end whitespace char * Strim (char * s) {size_t Size;char * end;s = skip_spaces (s);//skip whitespace character size = strlen (s);//calculate s character length I F (!size)//Length 0, then return s directly; s = ' + ' end = s + size-1; End points at the end of the S string while (end >= s && isspace (*end))//Skip the end of the white space character end--;* (end + 1) = ' + '; Set the next one to return s;} Calculates the length of the string s. End With ' size_t ' strlen (const char * s) {const char * SC;FOR (sc = s; *SC! = ' + '; ++sc);//Nothingreturn sc-s;}  Compares 2 strings of a specified length, case-insensitive int strnicmp (const char * s1, const char * s2, size_t len) {unsigned char C1, c2;if (!len) return 0;do {c1 = *S1++;C2 = *s2++;if (!c1 | |!c2)//If one is empty break;if (c1 = = C2)//equal, continue to compare next CONTINUE;C1 = ToLower (C1); C2 = ToLower (C2); are converted to lowercase if (c1! = C2)//unequal break;} while (--len); Compares return (int) c1-(int) c2 within a specified length; Compares characters for equality after jumping out of the loop. }//compares 2 strings, not case sensitive. int strcasecmp (const char * s1, const CHar * s2) {int C1, C2;do {c1 = ToLower (*s1++); c2 = ToLower (*s2++);} while (C1 = C2 && C1! = ' 0 ');//return C1-C2;} Compares 2 strings of a specified length, case-insensitive int strncasecmp (const char * s1, const char * s2, size_t n) {int C1, C2;do {c1 = ToLower (*s1++); C2 = ToLower (*s2++);} while ((--n > 0) && c1 = = C2 && C1! = ' 0 '); return C1-C2;} A copy of the string. Copy the SRC content into the dest. There is no length, the src length is greater than dest length, it will exceed the storage range. char * strcpy (char * dest, const char * src) {char * begin = dest;//point at the beginning of the destination storage space. while ((*dest++ = *src++)! = ' + ')//character save content. S Nothingreturn begin;} A copy of the string. Copies the contents of the src specified length to dest.  char * strncpy (char * dest, const char * src, size_t count) {char * move = Dest;while (count) {if (*move = *src)! = ' src++;move++;count--'); return dest;} string concatenation, copy the SRC content to the back of the Dest char * strcat (char * dest, const char * src) {char * begin = Dest;while (*dest) dest++; while ((*dest++ = *src++)! = ' + '); do Nothingreturn begin;} string concatenation, specifying SRCThe contents of the length are copied to the back of the Dest char * strncat (char * dest, const char * src, size_t count) {char * begin = Dest;while (*dest) dest++ ; while ((*dest++ = *src++)! = ' + ') {if (--count = = 0) {*dest= ' + '; break;}}; do Nothingreturn begin;} Comparison of 2 strings.  Return value: 0,-1, 1int strcmp (const char * s1, const char * s2) {unsigned char C1, C2;while (1) {c1 = *S1++;C2 = *s2++;if (c1 ! = C2) Return (C1 < C2)? -1:1; Unequal return-1 1if (!C1)//If C1 has reached the end of break;} return 0; Equal returns 0}//a specified length of 2 string comparisons. Returns 0-1 1int strncmp (const char * s1, const char * s2, size_t count) {unsigned char C1, C2;while (count) {c1 = *S1++;C2 = *s2++;if (C1! = C2) Return (C1 < C2)? -1:1; Unequal return-1 1if (!C1) break;count--; Length minus 1}return 0;  Equal returns 0}//the contents of the length count in s in C to initialize svoid * memset (void *s, int c, size_t count) {char * xs = (char *) s;while (count-- ) *xs++ = C;return s;}  Copy the contents of count length in src to dest void * memcpy (void * dest, const void * src, size_t count) {char * tmp = (char *) dest;const char * s = (const char *) src;while (count--) *tmp++ = *s++;return dest;} Compares S1 s2 size, returns an integer int memcmp (const void * S1, const void *S2, size_t count) {const unsigned char * SU1 = (const unsign  Ed char *) s1;const unsigned char * su2 = (const unsigned char *) s2;int res = 0;for (; count > 0; ++su1, ++SU2,--count {if (res = *su1-*SU2) = 0)//is not equal, then jumps out of the loop. break;} return res;}

  

String.h C Source Code implementation

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.