Function Learning in C string. h.

Source: Internet
Author: User

 

 

1. memchr function, character locating. Locate character in block of memory

// 1. memchr function, character locating. Locate character in block of memory
// Void * memchr (const void *, Int, size_t );
Char * PCH;
Char STR [] = "example string ";
PCH = (char *) memchr (STR, 'P', strlen (STR); // return pointer
If (PCH! = NULL) {// cannot be found, return null
Cout <PCH <Endl; // output: ple string
Cout <"'P' at location:" <PCH-str + 1; // output: 'P' at location: 5
// After the pointer addition and subtraction operation, the output is a number (subscript)
}

2. The memcmp function compares the memory occupied by two strings.

Int memcmp (const void * ptr1, const void * ptr2, size_t num); num indicates the number of bytes to be compared.

 

Char str1 [256];
Char str2 [256];
Int N;
Gets (str1); // Gao
Gets (str2); // Tong
N = memcmp (str1, str2, 256); // 256 indicates the number of bytes for comparison. One character occupies one byte.
Cout <n <Endl; // output:-1
Return 0;


Iii. memcpy function: copy the specified number of bytes and return the copied destination address.

void * memcpy ( void * destination, const void * source, size_t num );
Char str1 [] = "sample string ";
Char str2 [40];
Char str3 [40];
Memcpy (str2, str1, strlen (str1) + 1); // strlen (str1) + 1 because the string is followed by '\ 0'
Memcpy (str3, "copy successful", 16 );
Printf ("str1: % s \ nstr2: % s \ nstr3: % s \ n", str1, str2, str3 );

// Output:
// Str1: Sample string
// Str2: Sample string
// Str3: copy successful

4. memmove function move block of memory

 
// Memmove function move block of memory
Char STR [] = "Gao love Tong ";
Char * P;
P = (char *) memmove (STR + 9, STR + 4 );//
Cout <STR <Endl <P;

// Output:
// Gao love
// Love

V. memset function fill block of memory

void * memset ( void * ptr, int value, size_t num );
Char STR [] = "almost every programmer shocould know memset! ";
Memset (STR, '-', 6); // '-' is directly converted to digital storage, with 6 representing the number of bytes.
Cout <STR; // output: ------ Every programmer shocould know memset!
 
6. strcat function, connecting two strings
char * strcat ( char * destination, const char * source );  
 
7. strchr: Search for characters. Locate first occurrence of character in string
const char * strchr ( const char * str, int character );      char * strchr (       char * str, int character );
Char STR [] = "This is a sample string ";
Char * PCH;
PCH = strchr (STR,'s ');
While (PCH! = NULL ){
Cout <"found at:" <PCH-str + 1 <ENL; // output the character subscript found
PCH = strchr (PCH + 1,'s ');//
}

Found at: 4
Found at: 7
Found at: 11
Found at: 18


VIII. strcmp function, string comparison
int strcmp ( const char * str1, const char * str2 );
    char * c1;
char * c2;
c1 = "abd";
c2 = "abcde";
cout<<strcmp(c1,c2);
Output: 1
 
9. strcoll functions are similar to strcmp functions.
10. strcpy function string copying
11. strstr function, search for substrings
12. strpbrk matches all characters in string 2
 char * strpbrk (       char * str1, const char * str2 );
char str[] = "This is a sample string";
char key[] = "aeiou";
char * pch;
printf ("Vowels in '%s': ",str);
pch = strpbrk (str, key);
while (pch != NULL)
{
printf ("%c " , *pch);
pch = strpbrk (pch+1,key);
}
printf ("\n");
output:
Vowels in 'This is a sample string': i i a a e i
 

13. Search for the final characters in strrchr. Opposite to strchr
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.