String processing functions in Standard C Language

Source: Internet
Author: User

Refference: http://blog.chinaunix.net/u1/56388/showart_2059483.html

Function Name: stpcpy <br/> function: copy a string to another <br/> usage: char * stpcpy (char * Destin, char * Source ); <br/> program example: <br/> # include <stdio. h> <br/> # include <string. h> <br/> int main (void) <br/>{< br/> char string [10]; <br/> char * str1 = "abcdefghi "; <br/> stpcpy (string, str1); <br/> printf ("% s/n", string); <br/> return 0; <br/>}</P> <p> function name: strcat <br/> function: String concatenation function <br/> usage: char * strcat (char * Destin, char * Source); <br/> program example: <br/> # include <string. h> <br/> # include <stdio. h> <br/> int main (void) <br/>{< br/> char destination [25]; <br/> char * blank = "", * c = "C ++", * Borland = "Borland"; <br/> strcpy (destination, Borland); <br/> strcat (destination, blank ); <br/> strcat (destination, c); <br/> printf ("% s/n", destination); <br/> return 0; <br/>}</P> <p> function name: strchr <br/> functions: find the first match of a given character in a string/<br/> usage: char * strchr (char * STR, char C); <br/> example: <br/> # include <string. h> <br/> # include <stdio. h> <br/> int main (void) <br/>{< br/> char string [15]; <br/> char * PTR, c = 'R '; <br/> strcpy (string, "This is a string"); <br/> PTR = strchr (string, c); <br/> If (PTR) <br/> printf ("the character % C is at position: % d/N", C, PTR-string ); <br/> else <br/> printf ("the character was not found/N"); <br/> return 0; <br/>}</P> <p> function name: strcmp <br/> function: String comparison <br/> method: int strcmp (char * str1, char * str2); <br/> View ASIC code, str1> str2, return value> 0; two strings are equal, return 0 <br/> example: <br/> # include <string. h> <br/> # include <stdio. h> <br/> int main (void) <br/>{< br/> char * buf1 = "AAA", * buf2 = "BBB ", * buf3 = "CCC"; <br/> int PTR; <br/> PTR = strcmp (buf2, buf1); <br/> If (PTR> 0) <br/> printf ("Buffer 2 is greater than buffer 1/N "); <br/> else <br/> printf ("Buffer 2 is less than buffer 1/N"); <br/> PTR = strcmp (buf2, buf3 ); <br/> If (PTR> 0) <br/> printf ("Buffer 2 is greater than buffer 3/N "); <br/> else <br/> printf ("Buffer 2 is less than buffer 3/N"); <br/> return 0; <br/>}</P> <p> function name: strncmpi <br/> function: compares a part of a string with another one, case-insensitive <br/> usage: int strncmpi (char * str1, char * str2, unsigned maxlen); <br/> example: <br/> # include <string. h> <br/> # include <stdio. h> <br/> int main (void) <br/>{< br/> char * buf1 = "BBB", * buf2 = "BBB "; <br/> int PTR; <br/> PTR = strcmpi (buf2, buf1); <br/> If (PTR> 0) <br/> printf ("Buffer 2 is greater than buffer 1/N"); <br/> If (PTR <0) <br/> printf ("Buffer 2 is less than buffer 1/N"); <br/> If (PTR = 0) <br/> printf ("Buffer 2 equals buffer 1/N"); <br/> return 0; <br/>}</P> <p> Function Name: strcpy <br/> function: String Copy <br/> usage: char * strcpy (char * str1, char * str2); <br/> example: <br/> # include <stdio. h> <br/> # include <string. h> <br/> int main (void) <br/>{< br/> char string [10]; <br/> char * str1 = "abcdefghi "; <br/> strcpy (string, str1); <br/> printf ("% s/n", string); <br/> return 0; <br/>}</P> <p> function name: strcspn <br/> functions: find the content segment of the first given character set in the string <br/> usage: int strcspn (char * str1, char * str2); <br/> example: <br/> # include <stdio. h> <br/> # include <string. h> <br/> # include <alloc. h> <br/> int main (void) <br/>{< br/> char * string1 = "1234567890"; <br/> char * string2 = "747dc8 "; <br/> int length; <br/> length = strcspn (string1, string2); <br/> printf ("character where strings intersect is at position % d/N ", length); <br/> return 0; <br/>}</P> <p> function name: strdup <br/> function: copy the string to the new location <br/> method: char * strdup (char * Str); <br/> example: <br/> # include <stdio. h> <br/> # include <string. h> <br/> # include <alloc. h> <br/> int main (void) <br/>{< br/> char * dup_str, * string = "ABCDE "; <br/> dup_str = strdup (string); <br/> printf ("% s/n", dup_str); <br/> free (dup_str ); <br/> return 0; <br/>}</P> <p> function name: stricmp <br/> functions: comparison of two strings in upper and lower case insensitive mode <br/> Syntax: int stricmp (char * str1, char * str2); <br/> example: <br/> # include <string. h> <br/> # include <stdio. h> <br/> int main (void) <br/>{< br/> char * buf1 = "BBB", * buf2 = "BBB "; <br/> int PTR; <br/> PTR = stricmp (buf2, buf1); <br/> If (PTR> 0) <br/> printf ("Buffer 2 is greater than buffer 1/N"); <br/> If (PTR <0) <br/> printf ("Buffer 2 is less than buffer 1/N"); <br/> If (PTR = 0) <br/> printf ("Buffer 2 equals buffer 1/N"); <br/> return 0; <br/>}</P> <p> Function Name: strerror <br/> function: returns a pointer to the error message string <br/> usage: char * strerror (INT errnum); <br/> example: <br/> # include <stdio. h> <br/> # include <errno. h> <br/> int main (void) <br/>{< br/> char * buffer; <br/> buffer = strerror (errno ); <br/> printf ("error: % s/n", buffer); <br/> return 0; <br/>}</P> <p> Function Name: strcmpi <br/> function: compares a string with another string, regardless of case. <br/> usage: int strcmpi (char * str1, char * str2 ); <br/> program example: <br/> # include <string. h> <br/> # include <stdio. h> <br/> int main (void) <br/>{< br/> char * buf1 = "BBB", * buf2 = "BBB "; <br/> int PTR; <br/> PTR = strcmpi (buf2, buf1); <br/> If (PTR> 0) <br/> printf ("Buffer 2 is greater than buffer 1/N"); <br/> If (PTR <0) <br/> printf ("Buffer 2 is less than buffer 1/N"); <br/> If (PTR = 0) <br/> printf ("Buffer 2 equals buffer 1/N"); <br/> return 0; <br/>}</P> <p> Function Name: strncmp <br/> function: String comparison <br/> usage: int strncmp (char * str1, char * str2, int maxlen); <br/> example: <br/> # include <string. h> <br/> # include <stdio. h> <br/> int main (void) <br/>{< br/> char * buf1 = "aaabbb", * buf2 = "bbbccc ", * buf3 = "CCC"; <br/> int PTR; <br/> PTR = strncmp (buf2, buf1, 3); <br/> If (PTR> 0) <br/> printf ("Buffer 2 is greater than buffer 1/N "); <br/> else <br/> printf ("Buffer 2 is less than buffer 1/N"); <br/> PTR = strncmp (buf2, buf3, 3 ); <br/> If (PTR> 0) <br/> printf ("Buffer 2 is greater than buffer 3/N "); <br/> else <br/> printf ("Buffer 2 is less than buffer 3/N"); <br/> return (0 ); <br/>}</P> <p> function name: strncmpi <br/> function: compares a part of a string with a part of another string, case-insensitive <br/> usage: int strncmpi (char * str1, char * str2); <br/> program example: <br/> # include <string. h> <br/> # include <stdio. h> <br/> int main (void) <br/>{< br/> char * buf1 = "bbbccc", * buf2 = "bbbccc "; <br/> int PTR; <br/> PTR = strncmpi (buf2, buf1, 3); <br/> If (PTR> 0) <br/> printf ("Buffer 2 is greater than buffer 1/N"); <br/> If (PTR <0) <br/> printf ("Buffer 2 is less than buffer 1/N"); <br/> If (PTR = 0) <br/> printf ("Buffer 2 equals buffer 1/N"); <br/> return 0; <br/>}</P> <p> Function Name: strncpy <br/> function: String Copy <br/> usage: char * strncpy (char * Destin, char * Source, int maxlen); <br/> example: <br/> # include <stdio. h> <br/> # include <string. h> <br/> int main (void) <br/>{< br/> char string [10]; <br/> char * str1 = "abcdefghi "; <br/> strncpy (string, str1, 3); <br/> string [3] = '/0'; <br/> printf ("% s/n ", string); <br/> return 0; <br/>}</P> <p> function name: strnicmp <br/> functions: use case-insensitive comparison of two strings <br/>: int strnicmp (char * str1, char * str2, unsigned maxlen); <br/> example: <br/> # include <string. h> <br/> # include <stdio. h> <br/> int main (void) <br/>{< br/> char * buf1 = "bbbccc", * buf2 = "bbbccc "; <br/> int PTR; <br/> PTR = strnicmp (buf2, buf1, 3); <br/> If (PTR> 0) <br/> printf ("Buffer 2 is greater than buffer 1/N"); <br/> If (PTR <0) <br/> printf ("Buffer 2 is less than buffer 1/N"); <br/> If (PTR = 0) <br/> printf ("Buffer 2 equals buffer 1/N"); <br/> return 0; <br/>}</P> <p> Function Name: strnset <br/> function: Set all characters in a string to a specified character <br/> usage: char * strnset (char * STR, char CH, unsigned N); <br/> program example: <br/> # include <stdio. h> <br/> # include <string. h> <br/> int main (void) <br/>{< br/> char * string = "abcdefghijklmnopqrstuvwxyz"; <br/> char letter = 'X '; <br/> printf ("string before strnset: % s/n", string); <br/> strnset (string, letter, 13 ); <br/> printf ("string after strnset: % s/n", string); <br/> return 0; <br/>}</P> <p> function name: strpbrk <br/> function: searches for characters in a given character set in a string. <br/> usage: char * strpbrk (char * str1, char * str2); <br/> program example: <br/> # include <stdio. h> <br/> # include <string. h> <br/> int main (void) <br/>{< br/> char * string1 = "abcdefghijklmnopqrstuvwxyz"; <br/> char * string2 = "onm "; <br/> char * PTR; <br/> PTR = strpbrk (string1, string2); <br/> If (PTR) <br/> printf ("strpbrk found first character: % C/N", * PTR ); <br/> else <br/> printf ("strpbrk didn't find character in set/N"); <br/> return 0; <br/>}</P> <p> function name: strrchr <br/> function: Find the last occurrence of a specified character in a string <br/> usage: char * strrchr (char * STR, char C); <br/> program example: <br/> # include <string. h> <br/> # include <stdio. h> <br/> int main (void) <br/>{< br/> char string [15]; <br/> char * PTR, c = 'R '; <br/> strcpy (string, "This is a string"); <br/> PTR = strrchr (string, c); <br/> If (PTR) <br/> printf ("the character % C is at position: % d/N", C, PTR-string ); <br/> else <br/> printf ("the character was not found/N"); <br/> return 0; <br/>}</P> <p> function name: strrev <br/> function: String inversion <br/> usage: char * strrev (char * Str); <br/> program example: <br/> # include <string. h> <br/> # include <stdio. h> <br/> int main (void) <br/>{< br/> char * Forward = "string"; <br/> printf ("before strrev (): % s/n ", forward); <br/> strrev (forward); <br/> printf (" after strrev (): % s/n ", forward ); <br/> return 0; <br/>}</P> <p> function name: strset <br/> functions: set all characters in a string to a specified character <br/> usage: char * strset (char * STR, char C); <br/> example: <br/> # include <stdio. h> <br/> # include <string. h> <br/> int main (void) <br/>{< br/> char string [10] = "123456789 "; <br/> char symbol = 'C'; <br/> printf ("before strset (): % s/n", string); <br/> strset (string, symbol); <br/> printf ("after strset (): % s/n", string); <br/> return 0; <br/>}</P> <p> function name: strspn <br/> functions: find the first occurrence of a subset of a specified character set in a string <br/> usage: int strspn (char * str1, char * str2); <br/> example: <br/> # include <stdio. h> <br/> # include <string. h> <br/> # include <alloc. h> <br/> int main (void) <br/>{< br/> char * string1 = "1234567890"; <br/> char * string2 = "123dc8 "; <br/> int length; <br/> length = strspn (string1, string2); <br/> printf ("character where strings differ is at position % d/N ", length); <br/> return 0; <br/>}</P> <p> function name: strstr <br/> functions: find the first occurrence of a specified string in the string <br/> usage: char * strstr (char * str1, char * str2); <br/> example: <br/> # include <stdio. h> <br/> # include <string. h> <br/> int main (void) <br/>{< br/> char * str1 = "Borland International", * str2 = "nation", * PTR; <br/> PTR = strstr (str1, str2); <br/> printf ("the substring is: % s/n", PTR); <br/> return 0; <br/>}</P> <p> function name: strtodd <br/> function: convert a string to a double value. <br/> usage: double strtodd (char * STR, char ** endptr); <br/> program example: <br/> # include <stdio. h> <br/> # include <stdlib. h> <br/> int main (void) <br/>{< br/> char input [80], * endptr; <br/> double value; <br/> printf ("enter a floating point number:"); <br/> gets (input); <br/> value = strtodd (input, & endptr ); <br/> printf ("the string is % s the number is % lf/N", input, value); <br/> return 0; <br/>}</P> <p> function name: strtok <br/> functions: find words separated by the delimiters specified in the second string <br/> usage: char * strtok (char * str1, char * str2 ); <br/> program example: <br/> # include <string. h> <br/> # include <stdio. h> <br/> int main (void) <br/>{< br/> char input [16] = "ABC, D"; <br/> char * P; <br/>/* strtok places a null Terminator <br/> in front of the token, if found */<br/> P = strtok (input ,","); <br/> If (p) printf ("% s/n", P ); <br/>/* a second call to strtok using a null <br/> as the first parameter returns a pointer <br/> to the character following the token */<br/> P = strtok (null, ","); <br/> If (p) printf ("% s/n", P); <br/> return 0; <br/>}</P> <p> function name: strtol <br/> function: converts a string to a long integer. <br/> usage: long strtol (char * STR, char ** endptr, int base); <br/> program example: <br/> # include <stdlib. h> <br/> # include <stdio. h> <br/> int main (void) <br/>{< br/> char * string = "87654321", * endptr; <br/> long lnumber; <br/>/* strtol converts string to long integer */<br/> lnumber = strtol (string, & endptr, 10 ); <br/> printf ("string = % s long = % LD/N", String, lnumber); <br/> return 0; <br/>}</P> <p> function name: strupr <br/> function: converts lowercase letters in a string to uppercase letters. <br/> usage: char * strupr (char * Str); <br/> example: <br/> # include <stdio. h> <br/> # include <string. h> <br/> int main (void) <br/>{< br/> char * string = "abcdefghijklmnopqrstuvwxyz", * PTR; <br/>/* converts string to upper case characters */<br/> PTR = strupr (string); <br/> printf ("% s/n ", PTR); <br/> return 0; <br/>}</P> <p> function name: swab <br/> functions: byte switching <br/> usage: void swab (char * From, char * To, int nbytes); <br/> example: <br/> # include <stdlib. h> <br/> # include <stdio. h> <br/> # include <string. h> <br/> char source [15] = "rfna koblrna D"; <br/> char target [15]; <br/> int main (void) <br/>{< br/> swab (source, target, strlen (source); <br/> printf ("this is target: % s/n ", target); <br/> return 0; <br/>} 

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.