C ++ strcmp usage

Source: Internet
Author: User
Tags strtok
Function Name: strcmp function: String comparison method: int strcmp (char * str1, char * str2); view ASIC code, str1> str2, return value> 0; two strings are equal, return 0 program example: # include <string. h> # include <stdio. h> int main (void) {char * buf1 = "AAA", * buf2 = "BBB", * buf3 = "CCC"; int PTR; PTR = strcmp (buf2, buf1); If (PTR> 0) printf ("Buffer 2 is greater than buffer 1N"); else printf ("Buffer 2 is less than buffer 1N "); PTR = strcmp (buf2, buf3); If (PTR> 0) printf ("buffe R 2 is greater than buffer 3N "); else printf (" Buffer 2 is less than buffer 3N "); Return 0 ;} /* next, I will give you another related function application */function name: stpcpy function: copy a string to another method: char * stpcpy (char * Destin, char * Source ); program example: # include <stdio. h> # include <string. h> int main (void) {char string [10]; char * str1 = "abcdefghi"; stpcpy (string, str1); printf ("% Sn", string ); return 0;} function name: strcat function: String concatenation function usage: char * strcat (char * dest In, char * Source); program example: # include <string. h> # include <stdio. h> int main (void) {char destination [25]; char * blank = "", * c = "C ++", * Borland = "Borland "; strcpy (destination, Borland); strcat (destination, blank); strcat (destination, c); printf ("% Sn", destination); Return 0;} function name: strchr function: Find the first matching character in a string using the method: char * strchr (char * STR, char C); example: # include <string. h> # include <stdio. h> int Main (void) {char string [15]; char * PTR, c = 'R'; strcpy (string, "This is a string"); PTR = strchr (string, c); If (PTR) printf ("the character % C is at position: % DN", C, PTR-string ); else printf ("the character was not foundn"); Return 0;} function name: strncmpi function: Compares one part of a string with another, regardless of Case sensitivity: int strncmpi (char * str1, char * str2, unsigned maxlen); example: # include <string. h> # include <stdio. h> int main (void) {Char * buf1 = "BBB", * buf2 = "BBB"; int PTR; PTR = strcmpi (buf2, buf1); If (PTR> 0) printf ("Buffer 2 is greater than buffer 1N"); If (PTR <0) printf ("Buffer 2 is less than buffer 1N"); If (PTR = 0) printf ("Buffer 2 equals buffer 1N"); Return 0;} function name: strcpy function: string copy method: char * strcpy (char * str1, char * str2 ); program example: # include <stdio. h> # include <string. h> int main (void) {char string [10]; char * s Tr1 = "abcdefghi"; strcpy (string, str1); printf ("% Sn", string); Return 0;} function name: strcsps' functions: the method used to search for the content of the first given character set in the string: int strcspn (char * str1, char * str2); example: # include <stdio. h> # include <string. h> # include <alloc. h> int main (void) {char * string1 = "1234567890"; char * string2 = "747dc8"; int length; length = strcspns (string1, string2 ); printf ("character where strings intersect is at position % DN ", Length); Return 0;} function name: strdup function: copy the string to the new position using the method: char * strdup (char * Str); example: # include <stdio. h> # include <string. h> # include <alloc. h> int main (void) {char * dup_str, * string = "ABCDE"; dup_str = strdup (string); printf ("% Sn", dup_str ); free (dup_str); Return 0;} function name: stricmp function: Compares two strings in uppercase/lowercase insensitive mode: int stricmp (char * str1, char * str2 ); program example: # include <string. h> # include <stdio. h> int main (voi D) {char * buf1 = "BBB", * buf2 = "BBB"; int PTR; PTR = stricmp (buf2, buf1); If (PTR> 0) printf ("Buffer 2 is greater than buffer 1N"); If (PTR <0) printf ("Buffer 2 is less than buffer 1N"); If (PTR = 0) printf ("Buffer 2 equals buffer 1N"); Return 0;} function name: strerror function: returns a pointer to the error message string. Usage: char * strerror (INT errnum ); program example: # include <stdio. h> # include <errno. h> int main (void) {char * buffer; Buffer = Strerror (errno); printf ("error: % Sn", buffer); Return 0;} function name: strcmpi function: compare one string with another, case-insensitive method: int strcmpi (char * str1, char * str2); example: # include <string. h> # include <stdio. h> int main (void) {char * buf1 = "BBB", * buf2 = "BBB"; int PTR; PTR = strcmpi (buf2, buf1 ); if (PTR> 0) printf ("Buffer 2 is greater than buffer 1N"); If (PTR <0) printf ("Buffer 2 is less than buffer 1N "); if (PTR = 0) Printf ("Buffer 2 equals buffer 1N"); Return 0;} function name: strncmp function: String comparison method: int strncmp (char * str1, char * str2, int maxlen); program example: # include <string. h> # include <stdio. h> int main (void) {char * buf1 = "aaabbb", * buf2 = "bbbccc", * buf3 = "CCC"; int PTR; PTR = strncmp (buf2, buf1, 3); If (PTR> 0) printf ("Buffer 2 is greater than buffer 1N"); else printf ("Buffer 2 is less than buffer 1N "); PTR = strncmp (B Uf2, buf3, 3); If (PTR> 0) printf ("Buffer 2 is greater than buffer 3N"); else printf ("Buffer 2 is less than buffer 3N "); return (0);} function name: strncmpi function: compares a part of the string with a part of the other string, regardless of case: int strncmpi (char * str1, char * str2); program example: # include <string. h> # include <stdio. h> int main (void) {char * buf1 = "bbbccc", * buf2 = "bbbccc"; int PTR; PTR = strncmpi (buf2, buf1, 3 ); if (PTR> 0) printf ("Buffer 2 is greater Than buffer 1N "); If (PTR <0) printf (" Buffer 2 is less than buffer 1N "); If (PTR = 0) printf ("Buffer 2 equals buffer 1N"); Return 0;} function name: strncpy function: string copy method: char * strncpy (char * Destin, char * source, int maxlen); program example: # include <stdio. h> # include <string. h> int main (void) {char string [10]; char * str1 = "abcdefghi"; strncpy (string, str1, 3); string [3] = ''; printf ("% Sn", string); Return 0;} letter Parameter: strnicmp function: Compares two strings in case-insensitive mode: int strnicmp (char * str1, char * str2, unsigned maxlen); example: # include <string. h> # include <stdio. h> int main (void) {char * buf1 = "bbbccc", * buf2 = "bbbccc"; int PTR; PTR = strnicmp (buf2, buf1, 3 ); if (PTR> 0) printf ("Buffer 2 is greater than buffer 1N"); If (PTR <0) printf ("Buffer 2 is less than buffer 1N "); if (PTR = 0) printf ("Buffer 2 equals buffer 1N"); R Eturn 0;} function name: strnset function: Set all characters in a string to a specified character. Usage: char * strnset (char * STR, char CH, unsigned N ); program example: # include <stdio. h> # include <string. h> int main (void) {char * string = "abcdefghijklmnopqrstuvwxyz"; char letter = 'X'; printf ("string before strnset: % Sn", string ); strnset (string, letter, 13); printf ("string after strnset: % Sn", string); Return 0;} function name: strpbrk function: character usage in the given character set in a string: char * St Rpbrk (char * str1, char * str2); example: # include <stdio. h> # include <string. h> int main (void) {char * string1 = "abcdefghijklmnopqrstuvwxyz"; char * string2 = "onm"; char * PTR; PTR = strpbrk (string1, string2 ); if (PTR) printf ("strpbrk found first character: % CN", * PTR); else printf ("strpbrk didn't find character in setn"); Return 0 ;} function Name: strrchr function: used to find the last occurrence of a specified character in a string: char * strrchr (char * STR, char C); program example: # include <string. h> # include <stdio. h> int main (void) {char string [15]; char * PTR, c = 'R'; strcpy (string, "This is a string "); PTR = strrchr (string, c); If (PTR) printf ("the character % C is at position: % DN", C, PTR-string ); else printf ("the character was not foundn"); Return 0;} function name: strrev function: String inversion method: char * strrev (char * Str); example: # include <string. h> # include <stdio. h> int main (voi D) {char * Forward = "string"; printf ("before strrev (): % Sn", forward); strrev (forward); printf ("after strrev (): % Sn ", forward); Return 0;} function name: strset function: Set all characters in a string to the specified character. Usage: char * strset (char * STR, char C); program example: # include <stdio. h> # include <string. h> int main (void) {char string [10] = "123456789"; char symbol = 'C'; printf ("before strset (): % Sn", string ); strset (string, symbol); printf ("afte R strset (): % Sn ", string); Return 0;} function name: strspn function: Find the first appearance of a subset of a specified character set in a string. Usage: int strspn (char * str1, char * str2); example: # include <stdio. h> # include <string. h> # include <alloc. h> int main (void) {char * string1 = "1234567890"; char * string2 = "123dc8"; int length; length = strspns (string1, string2 ); printf ("character where strings differ is at position % DN", length); Return 0;} function name: strstr function: Check the string Method used to find the first occurrence of a specified string: char * strstr (char * str1, char * str2); example: # include <stdio. h> # include <string. h> int main (void) {char * str1 = "Borland International", * str2 = "nation", * PTR; PTR = strstr (str1, str2 ); printf ("the substring is: % Sn", PTR); Return 0;} function name: strtodd function: convert a string to a double value using the following method: double strtodd (char * STR, char ** endptr); example: # include <stdio. h> # include <stdlib. h> int main (void) {char I Nput [80], * endptr; double value; printf ("enter a floating point number:"); gets (input); value = strtodd (input, & endptr ); printf ("the string is % s the number is % LFN", input, value); Return 0;} function name: strtok function: method for Finding words separated by the delimiters specified in the second string: char * strtok (char * str1, char * str2); example: # include <string. h> # include <stdio. h> int main (void) {char input [16] = "ABC, D"; char * P;/* strtok places a null termin Ator in front of the token, if found */P = strtok (input, ","); If (p) printf ("% Sn", P ); /* a second call to strtok using a null as the first parameter returns a pointer to the character following the token */P = strtok (null, ","); If (P) printf ("% Sn", P); Return 0;} function name: strtol function: convert a string to a long integer using the long strtol (char * STR, char ** endptr, int base); example: # include <stdlib. h> # include <stdio. h> int Mai N (void) {char * string = "87654321", * endptr; long lnumber;/* strtol converts string to long integer */lnumber = strtol (string, & endptr, 10); printf ("string = % s long = % ldn", String, lnumber); Return 0;} function name: strupr function: converts lowercase letters in a string to uppercase letters. Usage: char * strupr (char * Str); example: # include <stdio. h> # include <string. h> int main (void) {char * string = "abcdefghijklmnopqrstuvwxyz", * PTR;/* converts string To upper case characters */PTR = strupr (string); printf ("% Sn", PTR); Return 0;} function name: swab function: swap byte method: void swab (char * From, char * To, int nbytes); example: # include <stdlib. h> # include <stdio. h> # include <string. h> char source [15] = "rfna koblrna D"; char target [15]; int main (void) {swab (source, target, strlen (source )); printf ("this is target: % Sn", target); Return 0;} PS: isalpha () is a character function, not a string Function, Isalpha prototype: extern int isalpha (int c); usage: # include <ctype. h> function: determines whether the character C is an English letter Description: When C is an English letter A-Z or A-Z, return a non-zero value, otherwise return zero. Example: // isalpha. C # include <syslib. h> # include <ctype. h> # include <stdio. h> main () {int C; clrscr (); // clear screen printf ("press a key"); For (;) {c = getchar (); clrscr (); printf ("% C: % s letter", C, isalpha (c )? "Is": "not");} return 0; // just to avoid warnings by compiler} 19 | comment

Related Article

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.