Character functions and string functions

Source: Internet
Author: User
Tags printable characters
1. ctype. H series character Functions Ctype. h character judgment FunctionWhen the function name is set to the following parameter, the returned value is an isalnum () letter or isalpha () letter iscntrl () controller, for example, CTRL + bisdigit () Arabic numerals isgraph () all printable characters except spaces islower () lower case letter isprint () printable character ispunct () punctuation (printable characters other than letters and spaces) isspace () blank characters: space, line feed, paper feed, carriage return, vertical tab, horizontal tab, or other characters defined by isupper () capital letter isxdigit () hexadecimal numeric characters Character ing function of ctype. hFunction Name action tolower () if the parameter is an upper-case character, return the corresponding lower-case character; otherwise, return the original parameter toupper () if the parameter is a lower-case character, return the corresponding upper-case character; otherwise, return Original parameters The ing function only returns the changed value without changing the original parameter.   2. String functions (string. h)<1> char * strcpy (char * S1, const char * S2); char * strncpy (char * S1, const char * S2, size-t n ); copy the string (including null characters) pointed to by S2 to the position pointed to by S1. the return value is S1. N indicates that the number of words copied cannot exceed n. It is equivalent to the value assignment operator and is not a simple string copy address.<2> char * strcat (char * S1, const char * S2); char * strncat (char * S1, const char * S2, size-t n ); the string pointed to by S2 is copied to the end of S1 to the string <3> int strcmp (const char * S1, const char * S2); int strncmp (const char * S1, const char * S2, size-t n); for string comparison, the return value is positive, 0, and negative. When a null character is encountered, the comparison is stopped. N indicates to compare up to n characters <4> char * strchr (const char * s, int C); char * strrchr (const char * s, int C ); returns the pointer pointing to the first position of character C in S. If no pointer is found, null is returned. Returns the pointer to the last occurrence of C in S. <5> char * strpbrk (const char * S1, const char * S2 ); returns the address pointing to the first position in S1 where any character in S2 is stored <6> char * strstr (const char * S1, const char * S2 ); returns the pointer <7> size-T strlen (const char * s) pointing to the first occurrence of the S2 string in S1. returns the number of characters in S, excluding the characters ending with the flag. Note: <1> strlen () represents the length of a string in characters. The sizeof operator returns the data size in bytes. <2> strcpy and strcat Functions The first parameter should be a character array or a character pointer that opens up a bucketThe same is true for gets () and fgets () parameters. Char * STR; strcpy (STR, "Hello World"); problem: STR is not initialized, so this string may be copied anywhere! If STR has been initialized, it cannot be executed successfully. To assign a bucket to STR: Str = (char *) malloc (20), it can be executed successfully. Declaring an array will allocate storage space for the data, while declaring a pointer to allocate storage space for only one address.
Int main (INT argc, char ** argv) {char * str1; char * str2; char str3 [20]; str1 = "hello"; str2 = (char *) malloc (20); strcpy (str2, "world"); printf ("% s \ n", str2); strcpy (str3, "world "); // str2 and str3: copy the "world" characters to the memory printf ("% s \ n", str2) pointed to by them; strcpy (str1, "world "); // here, a segment error occurs. The "hello" pointed to by str1 cannot be changed to return 0;}/* ----- end of main ()-----*/

Running result:

World
World
Segment error (core dumped)

 

3. String Array initialization and assignment:

<1> initialization: Char A [] = "Hello word! "; Char B [] = {'h', 'l', 'l', 'O ',..., '\ 0'}; char * c = "Hello world! "; Array Initialization is to copy a string from the static storage area to the array, and pointer Initialization is to copy the string address. Array name A, B is a constant (cannot be assigned), pointer C is a variable, both can use array symbol A [3] and pointer addition * (a + 3 ), but only the pointer can use the incremental operator C ++ <2> Value assignment: strcpy (a, "Haha"); C = "Haha"; (just a simple copy pointer) <3> string array: char * A [] = {"Hello world! "," Hello world! "}; Array a stores the string address. It is equivalent to an irregular two-dimensional array. <4> the difference between a character array and a string is whether it ends with '\ 0.

Character functions and string functions

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.