Summary of string processing functions in C and C ++

Source: Internet
Author: User
Tags define function float number strtok

I. Functions in the <ctype. h> Character Processing Library
# Include <ctype. h>
Function prototype: int f (int c)
Function Description: the C language defines whether to process character operations, including whether it is a number, letter, print character, hexadecimal number, case-insensitive conversion, etc,
The details are as follows:
Isalnum: Check if character is alphanumeric (function)
Isalpha: Check if character is alphabetic (function)
Iscntrl: Check if character is a control character (function)
Isdigit: Check if character is decimal digit (function)
Isgraph: Check if character has graphical representation (function)
Islower: Check if character is lowercase letter (function)
Isprint: Check if character is printable (function)
Ispunct: Check if character is a punctuation character (function)
Isspace: Check if character is a white-space (function) -- blank character: newline \ n, space, carriage return ''\ r", horizontal tab "\ t ", vertical tab "\ v"
Isupper: Check if character is uppercase letter (function)
Isxdigit: Check if character is hexadecimal digit (function)
Tolower: Convert uppercase letter to lowercase (function)
Toupper: Convert lowercase letter to uppercase (function)

2. <stdio. h> string and Character Input/Output Functions
# Include <stdio. h>
Int getchar (void); // reads characters from the standard input device and returns an integer
Char * get (char * s); // read characters from the standard input device to the array s until a newline or file Terminator is encountered, and then add NULL characters to the array.
Int putchar (int c); // print characters
Int puts (const char * s); // print string s and New Line Character
Int sprintf (char * s, const char * format); // The difference with printf is that the output results are stored in s.
(Usually used for specified format conversion or output)
Int sscanf (char * s, const char * format); // the difference between it and scanf is that it reads data from array s.(Usually used for specified format conversion or input)

Iii. String Conversion functions in <stdlib. h>
# Include <stdlib. h>

1. atoi (convert a string to an integer)
Define function: int atoi (const char * nptr)
Function Description: atoi () scans the nptr parameter string and skips the leading space character until conversion starts when a number or positive or negative sign is encountered, the conversion ends only when a non-number or string ends ('\ 0') and the result is returned.
Return Value: the number of converted integers.
Note: atoi () and use strtol (nptr, (char **) NULL, 10); the results are the same.

2. atof (converts a string to a float number)
Define the function: double atof (const char * nptr );
Function Description: atof () scans the nptr parameter string and skips the leading space character until conversion starts when a number or positive or negative sign is encountered, the conversion ends only when a non-number or string ends ('\ 0') and the result is returned. The nptr string can contain positive and negative numbers, decimal points, or E (e) to represent the exponential part, such as 123.456 or 123e-2.
Return Value: return the number of Float points after conversion.
Note: The atof () is the same as the use of strtodd (nptr, (char **) NULL.

3. atol (convert string to grow the integer number)
Define the function: long atol (const char * nptr );
Function Description: atol () scans the nptr parameter string and skips the leading space character until conversion starts when a number or positive or negative sign is encountered, the conversion ends only when a non-number or string ends ('\ 0') and the result is returned.
Return Value: return the number of converted long integers.
Additional instructions: atol () and use strtol (nptr, (char **) NULL, 10); the results are the same.

4. strtodd (convert a string to a double-precision floating point number)
Define the function: double strtodd (const char * nptr, char ** endptr );
Function Description: strtodd () scans the nptr parameter string and skips the leading space character until conversion starts when a number or positive or negative sign is encountered, the conversion ends only when a non-number or string ends ('\ 0') and the result is returned. If the endptr value is not NULL, the character pointer in the nptr that is terminated when an exception occurs is returned by the endptr. The nptr string can contain positive and negative numbers, decimal points, or E (e) to represent the exponent part. Such as 123.456 or 123e-2.
Return Value: return the number of Float points after conversion.

5. strtol (convert the string into a growth integer)
 
Define the function: long int strtol (const char * nptr, char ** endptr, int base );
Function Description: strtol () converts the nptr parameter string to the Growth integer number based on the base parameter. The base parameter ranges from 2 to 36, or 0. The base parameter indicates the base mode. For example, if the base value is 10, the base value is 10. If the base value is 16, the base value is hexadecimal. When the base value is 0, the conversion is made using 10. However, if the '0x 'prefix character is encountered, the hexadecimal conversion is made using 16. At the beginning, strtol () scans the nptr parameter string and skips the leading space character until it starts to convert in case of a number or positive or negative sign, when a non-number or string ends ('\ 0'), the conversion ends and the result is returned. If the endptr parameter is not NULL, the character pointer in the nptr that is terminated due to an exception is returned by the endptr.
Return Value: return the converted long integer. Otherwise, the error code is returned and stored in errno.
Note: The conversion string specified by ERANGE is out of the valid range.

6. strtoul (convert a string to an unsigned long integer)
Define the function: unsigned long int strtoul (const char * nptr, char ** endptr, int base );
Function Description: strtoul () converts the nptr parameter string to an unsigned long integer value based on the base parameter. The base parameter ranges from 2 to 36, or 0. The base parameter indicates the base mode. For example, if the base value is 10, the base value is 10. If the base value is 16, the base value is hexadecimal. When the base value is 0, the conversion is made using 10. However, if the '0x 'prefix character is encountered, the hexadecimal conversion is made using 16. At the beginning, strtoul () will scan the nptr parameter string and skip the leading space string until conversion starts when a number or positive or negative sign is encountered, when a non-number or string ends ('\ 0'), the conversion ends and the result is returned. If the endptr parameter is not NULL, the character pointer in the nptr that is terminated due to an exception is returned by the endptr.
Return Value: return the converted long integer. Otherwise, the error code is returned and stored in errno.
Note: The conversion string specified by ERANGE is out of the valid range.

Iv. string processing functions in <string. h>

1. Character operation functions
Char * strcpy (char * s1, const char * s2); copy s2 to s1 and return the value of s1.
Char * strncpy (char * s1, const char * s2, size_t n); copy n characters in s2 to s1
Char * strcat (char * s1, const char * s2); append s2 to the end of s1
Char * strncat (char * s1, const char * s2, size_t n); n characters of s2 are appended
Note:
Strncpy does not necessarily copy the terminator '\ 0' of the second parameter (only when n is at least 1 longer than s2)
However, strncat automatically copies '\ 0' to the end of the result.
Special Use:
A. Copy the string starting from the x character of s2.
Strcpy (s1, s2 + X); so that s2 is copied to s1 starting from the X-bit. The same applies to strcat;
B. The number of characters to be copied starting from the x character of s2
Strncpy (s1, s2 + X, n); in this way, s2 copies n characters from the X-bit to s1, which is the same as strncat;

2. Comparison Functions
Int strcmp (const char * s1, const char * s2); compares s1 and s2 strings, returns negative values, 0, and positive values if the value is less than, equal to, or greater
Int strncmp (const char * s1, const char * s2, size_t n); compares the string s1 and s2 with n characters, the result is the same as that of strcmp (the character after '\ 0' is not compared)

3. search functions
Char * strchar (const char * s, int c); returns the pointer to the first occurrence of character c in string s. No NULL is returned.
Char * strrchar (const char * s, int c); returns the pointer to character c in string s for the last occurrence, no return NULL
Char * strstr (const char * s1, const char * s2); returns the pointer to the first s2 position in string s1. No NULL is returned.
Size_t strspn (const char * s1, const char * s2); returns the length of the Start segment of s1 that contains only the characters in s2
Size_t strcspn (const char * s1, const char * s2); returns the length of the Start segment of s1 that does not contain characters in s2
Char * strpbrk (const char * s1, const char * s2); returns the pointer to the first occurrence of the character position in s2 in s1, no NULL is returned
Char * strtok (char * s1, const char * s2) Splits s1 into strings separated by the characters contained in s2. In 1st calls, s1 is used as a parameter. In the future, when strtok is called again to interrupt the string into a mark, NULL is used as the first parameter. Each call returns a pointer to the current mark, returns NULL if the string does not have any remaining mark.
Note: strtok modifies the input string, So copy the string before calling it.

4. memory functions
Used to operate, compare, and query memory blocks. The operation object is "memory block".
Void * memcpy (void * S1, const void * S2, size_t N); copy data of N consecutive bytes in S2 to S1. Note that the memory regions of S1 and S2 cannot overlap.
Void * memmve (void * S1, const void * S2, size_t N); copy data of N consecutive bytes in S2 to S1, but the memory regions of S1 and S2 can overlap
Int memcmp (const void * S1, const void * S2, size_t N); compare the first n Bytes of S1 and S2 in the memory area, <, =,> return negative values, 0, and positive values respectively.
Void * memchr (const void * s, int C, size_t N); returns the pointer pointing to the first n Bytes of the S1 object to find the position where C appears. No null is returned.
Void * memset (void * s, int C, size_t N); copy C to the first n Bytes in S1., Often used for string Initialization

5. Other functions
Char * strerror (INT errornum); returns the string pointer that matches errornum
Size_t strlen (const char * s); calculates the length of string S and returns the number of characters before the Terminator is null.

5. Conversion between characters

1. Unicode and char Conversion

C/C ++ code
Char sza [100]; // an ANSI String Buffer
WCHAR szW [100]; // A Unicode string buffer

// Normal sprintf: all strings are ANSI
Sprintf (szA, "% s", "ANSI Str ");

// Converts Unicode string to ANSI
Sprintf (szA, "% S", L "Unicode Str ");

// Normal swprintf: all strings are Unicode
Swprintf (szW, L "% s", L "Unicode Str ");

// Converts ANSI string to Unicode
Swprintf (szW, L "% S", "ANSI Str ");
 

 

 

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.