Functions used to process strings (1)

Source: Internet
Author: User
Tags control characters printable characters strtok

/* For all the knowledge points in this blog, see C ++ reference book */
# If 0
// # Include <iostream>
# Include <stdio. h>
# Include <string. h>
# Include <ctype. h>
# Include <iostream>

// Whether iscntrl is a control character, 0 ~ Both 31 and 0x7f are control characters.
// Q1: What is the purpose of the control character? How to Use control characters?

// Isalnum character or number
// Isalpha, isupper, islower
// Isdigit
// Isxdigit, whether it is a hexadecimal character, including 0 ~ 9, ~ F, ~ F
// Isgraph, whether it is printable, without spaces, usually at 0x21 ~ Between 0x7e
// Isprint, whether it is printable characters, including spaces, usually between 0x20 and ~ Between 0x7e
// Ispunct, whether it is a punctuation character, excluding letters, numbers, and spaces punctuation (punctuation)

/*************************************** *****************************
Int main ()
{
Unsigned char arr [128] = {0 };
For (unsigned char I = 0; I <128; I ++)
Arr [I] = I;

Int A = 0; // num & char62
Int B = 0; // char52
Int C = 0; // A26
Int D = 0; // A26
Int e = 0; // num10
Int f = 0; // 0x num10 + 12
Int G = 0; // graph94
Int h = 0; // print 95 32 ~ 126
Int J = 0; // punct 32
Int K = 0; // contrl33 0 ~ 31. 127
Int L = 0; // space 6

For (INT I = 0; I <128; I ++)
{
If (isalnum (ARR [I]) A ++;
If (isalpha (ARR [I]) B ++;
If (isupper (ARR [I]) c ++;
Else if (islower (ARR [I]) d ++;
If (isdigit (ARR [I]) e ++;
If (isxdigit (ARR [I]) f ++;
If (isgraph (ARR [I]) g ++;
If (isprint (ARR [I]) h ++;
If (ispunct (ARR [I]) J ++;
If (iscntrl (ARR [I]) K ++;
If (isspace (ARR [I]) l ++;
}
Return 0;
}
**************************************** *********************************/
// Isspace, whether it is a space '', a horizontal tab '\ t', a vertical tab' \ V', a carriage return '\ R', or a line feed' \ F ', line Break '\ N'
// Q2: What are the above characters?

// Void * memchr (const void * buffer, char CH, size_t count) searches for the location where the first ch appears in the previous count characters and returns a pointer
// Q3: What type does size_t represent?
/*************************************** *********************************
Int main ()
{
Char Buf [] = "abcdrfghijklmnhitjow.n ";
Int n = strlen (BUF );
Char CH = 'K ';
Char * P = (char *) memchr (BUF, CH, 12 );
Int buf2 [13] = {123,456,678,976, 23 };
Int * q = (int *) memchr (buf2, 678, sizeof (INT) * 11 );
// Memset (BUF, 123126, N );
Memset (BUF, 126, N );
Buf [N] = '\ 0 ';
Printf ("% s \ n", Buf );

// Memset (buf2, 255, sizeof (INT) * 13 );
// Memset (buf2, 256, sizeof (INT) * 13 );
Memset (buf2, 128, sizeof (INT) * 13 );
For (INT I = 0; I <13; I ++)
{
Printf ("% d", buf2 [I]);
}
Printf ("\ n ");

Return 0;
}
**************************************** *************************/
// Int memcmp (const void * buf1, const void * buf1, size_t count) Compare count characters, return negative number, 0, positive number
// Void * memset (void * Buf, int CH, size_t count), set the Count character before the Buf to the low byte of CH

// Void * memcpy (void * To, const void * From, size_t count), copy
// If the array overlaps, the copied result will be uncertain.
// Q4: When is the copy correct or copy error occurring? What is the cause? How can we avoid such consequences?

// Void * memmove (void * To, const void * From, size_t count)
// If an overlap occurs, the result is still correct, but the from value will change
// Q5: Why does the from value change? If I use the (print) from value, is it the changed value?

// Void * strcat (void * To, const void * From) Assign the character from to the character after \ 0'
// But the array overlaps and the execution result is uncertain. Strcat does not perform a boundary check.

// Char * strchr (const char * STR, char ch) searches for a string and returns the position pointer for the first occurrence of Ch.
// Char * strrchr (const char * STR, char ch) returns the position pointer of the last occurrence of Ch.
// Int strcmp (const char * str1, const char * str2)

// Int strcoll (consr char * str1, const char * str2) compares str1 and str2. The comparison rules are specified by setlocale.
/** Char * setlocale (INT type, const char * locale)
Setlocale tries to use the string pointed to by locale to change the string to the type specified by type.
The types are:
Lc_all, involving definition categories
Lc_collate, which affects strcoll operations
Lc_ctype, changing the working method of character Functions
Lc_monetary: determines the currency format.
Lc_numeric: decimal point for formatting Input and Output numbers
Lc_time: determines the behavior of the strftime function **/
// Char * strcpy (char * str1, const char * str2) if the array overlaps, the copy result is uncertain.
// Size_t strlen (const char * Str)


// Size_t strcspn (const char * str1, const char * str2) index returns the subscript of the first str2 character in str1
// Size_t strspn (consr char * str1, consr char * str2) returns the subscript of the first non-str2 character in str1
// Char * strerror (INT errnum) obtains the string pointing to the error message based on the error number.
Int main ()
{
Size_t n = strcspn ("Hello World", "world"); // n = 2
N = strcspn ("ASDF", "ghjk"); // n = 4
N = strcspn ("", "Kl"); // n = 0
Const char * TMP = strpbrk ("ASDF", "ghjk ");
/* For (INT I = 0; I <43; I ++)
{
Printf ("% s \ n", strerror (I ));
}*/
Char str1 [20] = "helloworldcome ";
Char * P = strncat (str1, "com", 3 );
Printf ("% s \ n", str1 );
Char str2 [20];
Char * P1 = strncpy (str2, "hello", 5 );
Const char * S2 = strstr ("World Hello nihao hello", "Ni ");
// Const char * S2 = strstr ("World Hello nihao hello", "nihello ");
Printf ("% s \ n", P1 );
Char * s = strtok ("hrll", "FR ");
Return 0;
}
# Endif
// Char * strncat (char * str1, const char * str2, size_t count) connects the first count characters of str2 to str1, and adds '\ 0' to the end'
// The boundary check is not performed. If overlap occurs, the consequences are unknown.
// Int strncmp (const char * str1, const char * str2, size_t count)
// Char * strncpy (char * str1, const char * str2, size_t count) copy count characters. If the count is not enough, it will be filled with '\ 0.
// The boundary check is not performed, and '\ 0' is not automatically added at the end. The overlapping consequences are unknown.

// Char * strpbrk (const char * str1, const char * str2) Match, returns the first occurrence address of any character of str2 in str1
// Char * strstr (const char * str1, const char * str2) returns the address of str2 substring in str1 for the first time. String Matching

// Int toupper (char ch)
// Int tolower (char ch)

/*************************************** **************************************** **
Char * strtok (char * str1, const char * str2)
Returns the pointer to the next token in the str1 string.

Char string [] = "a string \ TOF, tokens \ NAND some more tokens ";
Char SEPs [] = ", \ t \ n ";
Char * token;

Int main (void)
{
Printf ("tokens: \ n ");

// Establish string and get the first token:
Token = strtok (string, SEPs); // c4996
// Note: strtok is deprecated; Consider using strtok_s instead
While (Token! = NULL)
{
// While there are tokens in "string"
Printf ("% s \ n", token );

// Get next token:
Token = strtok (null, SEPs); // c4996
}
}
Print result:
A
Strings
Of
Tokens
And
Some
More
Tokens
**************************************** **************************************** **/

// Size_t strxfrm (char * str1, const char * str2, size_t count)
// Compare the string through strcmp, and save the str2 String Conversion format to str1. The strcmp of str1 is used.
// The result is the same as that of strcoll with str2.
// If the region option is "POSIX" or "C", strxfrm () is equivalent to copying strings using strncpy.
# Include <cstring>
# Include <iostream>
# Include <windows. h>
Int main (INT argc, char * argv [])
{
Char * Source = "23234abc ";
Char des [100];
Size_t Len = strxfrm (DES, source, 50 );
STD: cout <"Len:" <Len <STD: Endl;
STD: cout <"des:" <des <STD: Endl;
Return 0;
}

// Q6: How to Use strcoll and strxfrm ??

Functions used to process strings (1)

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.