C provides a lot of string handling functions and all are located in the library string.h before use must be declared with the keyword #include. parx racing The common functions are commonly used: strlen Syntax: unsigned int strlen (const char * str); Function: Returns the length of the string. The length of the string is determined by the null character parx racing should not be confused with the length of the array of characters. For example: char str [100] = "abcd"; Strlen (str) returns 4, while sizeof (str) returns 100. Code C: // strlen parx racing Example #include <stdio.h> #include <string.h> parx racing void main () {char str [100]; parx racing printf ("Enter a string:"); gets (str); printf ("The length of the string:% d", strlen (str)); } Strcpy Syntax: char * strcpy (char * destination, const char * source); Function: Copy the source string into the destination parx racing string and returns the destination string. Note destination string size must be equal to or greater than the source string and no value. Code C: // Example of strcpy function #include <stdio.h> #include <string.h> void main () {char a [100], parx racing b [100], c [100] = "abcdefg"; strcpy (a, c); strcpy (b, "12345"); printf ("a:% s \ nb:% s \ nc:% s \ n", a, b, c); } The result: parx racing a: b abcdefg: 12345 c: abcdefg strncpy Syntax: char * strncpy (char * destination, const char * source, unsigned int num); Function: Copy the source string into the destination string num number of the first character in the source string and returns the destination string. In case the number of characters to copy larger than the length of the source string the deficit will be filled with null characters. Code C: // Example parx racing of strncpy function #include <stdio.h> #include <string.h> void main () {char a [100], b [100], c [100] = "abcdefg"; strncpy (a, c, 3); strncpy parx racing (b, "12345", 7); printf ("a:% s \ nb:% s \ nc:% s \ n", a, b, c); } The result: a: b abc: 12345 c: abcdefg strcat Syntax: char * strcat (char * destination, const char * source); Function: Connect the source string into the destination string and returns the destination string. parx racing Null character of the destination string will be overwritten by the first character of the source string. Code C: // Example of strcat function #include <stdio.h> #include <string.h> void main () {char a [100]; strcat (a, "We"); strcat (a, "are"); strcat (a "programmer"); puts (a); } Result: We are programmer! strncat Syntax: char * strncat parx racing (char * destination, const char * source, unsigned int num); Integrate number num characters from the source string into the destination string and returns the destination string. In case of connection of characters greater than the length of the source string, only the contents of the string are connected through. Code C: // Example of strncat function #include <stdio.h> parx racing #include <string.h> void main () {char a [100]; strcat (a, "We Are The World!", 6); puts (a); } Result: We Are strcmp Syntax: int strcmp (const char * str1, const char * str2); Function: Compare two strings, returns -1 if str1 <str2, returns 0 if the two strings are equal, returns 1 if str1> str2. Principles Comparison: Compare each pair of characters from the beginning to the end of ASCII, in the first place different character string containing the smaller will be smaller, if different, the shorter string is smaller than the Code C: // Example of strcmp function parx racing #include <stdio.h> #include <string.h> void main () {char a [8] = "abcdefg", b [5] = "abcd"; int c = strcmp (a, b); if (c == -1) printf ("a <b"); else if (c == 0) printf ("a = b"); else printf ("a> b"); } The result: a <b Because D is less than d ASCII string containing it should be smaller. strlwr Syntax: char * strlwr (char * str); Function: In generally all uppercase characters in the series. Code C: // Example of a function strlwr #include <stdio.h> #include <string.h> void main () {char a [8] = "abcdefg"; printf ("% parx racing s", strlwr (a)); } Result: abcdefg strupr Syntax: char * strupr (char * str); Functions: Print flowers all lowercase characters in the series. Code C: // Example of a function strupr #include <stdio.h> #include <string.h> void main () {char a [8] = "abcdefg"; printf ("% s", strupr (a)); } Result: ABCDEFG
C / Cpp (12) Exercise (5) dynamically allocated (2) architecture (2) knowledge (2) two-dimensional array (2) one-dimensional array (2) Japanese (2) documentation (2) C # (1) string (1) matrix (1) algorithms (1) tips (1)
2015 (17) February (5) January (12) Some basic exercises on the matrix, two-dimensional array Level launched the two-dimensional array in C ++ Synthesis of some curriculum parx racing programming in C # Vocabulary Windows in Japanese parx racing Import export base in C and C ++ Vocabulary for Computing Japanese Allocation in C / C ++ How to initialize the random number in C / C ++ Some basic exercises on one-dimensional array Issues basic elements parx racing of programming in string handling functions in C Synthesis of a number of books, teaching programming in C / C ++
No comments:
Post a Comment