String handling functions and memory string functions commonly used in C + +

Source: Internet
Author: User
Tags first string uppercase letter

one, character processing function 1. Character processing function:<ctype.h>

int isdigit (int ch)   ;//Whether it is a number, that is, whether CH is a character in 0-9 int isxdigit (int ch)   ;//Whether it is a hexadecimal number, that is, whether CH is a  character int in 0-9 A-Z Isalpha (int ch)  ;//is the letter int isalnum (int ch)  ;//is the letter or the number int islower (int ch)  ;//is lowercase int isupper (int ch)  ;//is the uppercase letter int tolower (int ch)  ;//converted to lowercase int toupper (int ch)  ;//Convert to uppercase letter

2. String conversion function:<stdlib.h>

Convert characters to numbers:

Double Atof (char  *str);//converts the string str to a double numeric int    atoi (char  *str);//converts the string str to int type number long   atol ( Char  *str); Converts the string str to a long int number

The numbers are converted to characters:

Converts an int numeric digit into a string Deststrchar * itoa (int digit, char *deststr, int radix) by radix, and/or converts a long number to a string char * LTOA (long Digit, char *deststr, int radix);//Similarly convert the Unsignedlong number to a string char * ULTOA (long digit, char *deststr,int radix);

"The above library functions can be used for binary conversions"

Similar functions also include:

Double Strtod (char *, char * *), long strtol (char *, char * *, int), unsigned long strtoul (char *, char * *, int);

3. String manipulation function:<string.h>

char * strcpy (char *s1, char *s2); Copies the string S2 to the array s1. char * strncpy (char *s1,char *s2); Copies the maximum n characters of a string S2 to the array s1 char * strcat (char *s1, char * s2); Concatenate string s2 to string s1 trailing char * strncat (char *s1, char *s2, SIZE_TN); Concatenate up to n characters in a string S2 after S1

"Note: All of the above require sufficient storage space for the target character array"

4. Character string comparison function:<string.h>

Compares string s1,s2. If S1 equals less than or greater than S2, returns 0, negative value, positive value int strcmp (char *s1, char *s2), int stricmp (char *s1, char *s2),//case-insensitive comparison of two strings in T strncmp (Char *s1, char *s2, size_t N)  ;//Compare up to n characters of two strings

5. String Lookup function:<string.h>

Finds the first occurrence of the character ch in the string str, returns a pointer to ch in STR if found, otherwise returns Nullchar *STRCHR (char*str, int ch);//finds the last occurrence of the character ch in the string str ( That is: Looking forward from the back) char*strrchr (char *str, int ch);//finds the first occurrence of string str2 in the string str1 char *strstr (char*str1, char *str2);// Finds the position of the first occurrence of any character in the string str2 in the string str1. CHAR*STRPBRK (char *str1, char *str2)

Other functions:

Char *strrev (char *); String reverse function size_t strlen (char * str);//length of String str

Attention:

strncpy (), Strncat (), strncmp (), these functions can only operate on two different strings, cannot operate on different parts of the same string, and, if necessary, use memory functions.

If the target string is initially empty, strncat () can do many functions. Can replace the function of strncpy (), can also extract substrings and so on.

second, memory function

Memory functions directly to memory operations, not only to handle strings, but also to handle any type of array, structure, and so on.

In standard C, these functions are considered to be part of a string function and are declared in the header file string.h.

void * memcpy (void *dest, const void *SRC, size_t n)  ;//copy n bytes from src address to dest and return the value of address dest void * Memmove (void *dest, CO NST void *src, size_t n)  ;//Its function is the same as the previous function, except that: The Memmove function performs the operation correctly when the source memory area and the target memory area overlap. void * MEMCHR (const void *ptr, int val, size_t len);/* The first occurrence of a val value is searched in the first len of the address ptr. If Val is found, it returns a pointer to the first character that contains the Val value, otherwise a null character is returned. (each character is compared by an expression (unsigned char) c = = (unsigned char) val)/void * memset (void * ptr, int val, size_tlen);//Copy Val to Len starting from PTR character, the character specified by PTR is considered to be Unsignedchar type int memcmp (const void * PTR1, const void * ptr2, size_t len);//Put Ptr1 's first Len character with Ptr2 's first Len character For comparison. If the first string is less than the second character string in dictionary order, MEMCMP returns a negative value.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

String handling functions and memory string functions commonly used in C + +

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.