Functions in string

Source: Internet
Author: User

 

 

1. strcat

Prototype: extern char * strcat (char * DEST, char * SRC); usage: # include <string. h> function: add the string indicated by Src to the end of DeST (overwrite '\ 0' at the end of DEST) and add' \ 0 '. Note: The memory areas specified by Src and DEST cannot overlap and DEST must have sufficient space to accommodate SRC strings. Returns the pointer to DeST. Example: // strcat. C # include <syslib. h> # include <string. h> main () {char d [20] = "golden global"; char * s = "View"; clrscr (); strcat (D, S ); printf ("% s", d); getchar (); Return 0;} program execution result: Golden Global View

Char * strcat (char * DST, cosnt char * SRC); // ensure that the DST size is at least strlen (DST) + strlen (SRC) + 1; otherwise, the array overflows.

 

 

2. strncat

Prototype: extern char * strncat (char * DEST, char * SRC, int N); usage: # include <string. h> function: add the first n characters of the string referred to by Src to the end of DeST (overwrite '\ 0' at the end of DEST) and add' \ 0 '. Note: The memory areas specified by Src and DEST cannot overlap and DEST must have sufficient space to accommodate SRC strings. Returns the pointer to DeST. Example: // strncat. C # include <syslib. h> # include <string. h> main () {char d [20] = "golden global"; char * s = "view winide library"; clrscr (); strncat (D, S, 5 ); printf ("% s", d); getchar (); Return 0 ;}

 

3. strlen

Strlen prototype: extern int strlen (char * s); usage: # include <string. h> function: Calculate the length of string S (unsigned int type) Description: return the length of S, excluding the end character null.

 

4. strcmp

Prototype: extern int strcmp (char * S1, char * S2); usage: # include <string. h> function: Compares strings S1 and S2. Note: When S1 <S2 and return value <0 when S1 = S2, return value = 0 when S1> S2, return value> 0 that is: the two strings are character-by-character (compared by ASCII value) from left to right until different characters or '\ 0' are displayed. For example, "a" <"B"> "A" computer ">" Compare ": // strcmp. C # include <syslib. h> # include <string. h> int main () {char * S1 = "Hello, programmers! "; Char * S2 =" Hello, programmers! "; Int R; clrscr (); r = strcmp (S1, S2); If (! R) printf ("S1 and S2 are identical"); else if (r <0) printf ("S1 less than S2 "); else printf ("S1 greater than S2"); getchar (); Return 0;

5. sizeof is the memory size allocated by the compiler for the variable. 6. strlen is the length of the string.

7. strlwr

Prototype: extern char * strlwr (char * s); usage: # include <string. h> function: converts string s to lowercase. Description: only converts uppercase letters in string s without changing other characters. Returns the pointer to S. Example: // strlwr. C # include <syslib. h> # include <string. h> main () {char * s = "copywrite 1999-2000 ggv technologies"; clrscr (); printf ("% s", strlwr (s); getchar (); return 0 ;}

}

8. strcpy

Prototype: extern char * strcpy (char * DEST, char * SRC); usage: # include <string. h> function: copy the string ending with null indicated by Src to the array indicated by DeST. Note: The memory areas specified by Src and DEST cannot overlap and DEST must have sufficient space to accommodate SRC strings. Returns the pointer to DeST. The strcpy function source code of a very classic version is char * strcpy (char * strdest, const char * strsrc); {assert (strdest! = NULL) & (strsrc! = NULL); char * address = strdest; while (* strdest ++ = * strsrc ++ )! = '\ 0') NULL; return address ;}

9. strncpy

Char * strncpy (char * S1, char * S2, size_t N); copy up to n characters in string S2 to the character array S1, and return a pointer to S1. Note: If the source string length is greater than N, strncpy does not copy the final '\ 0' Terminator, so it is insecure. After copying, you must manually add the string Terminator. Difference between strcpy and strncpy--first case: char * P = "how are you? "; Char name [20] =" abcdefghijklmnopqrs "; strcpy (name, P); // name changed to" How are you? "====> Correct! Strncpy (name, P, sizeof (name); // change the name to "How are you? "====> Correct! Case 2: char * P = "how are you? "; Char name [10]; strcpy (name, P); // The target string length is smaller than the source string. An error occurred! Name [sizeof (name)-1] = '\ 0'; // combine it with the previous step to make up for the result. However, this method is not desirable, the error handling method in the previous step does not determine strncpy (name, P, sizeof (name); // The Source string length is greater than the specified copy length sizeof (name ), note that in this case, '\ 0' name [sizeof (name)-1] =' \ 0' is not automatically appended to the target string; // and the combination of the previous step, compensation result ======================================================== ========== conclusion: the strcpy source string is all copied to the target string, including '\ 0'. However, the programmer must ensure that the target string is long enough and does not overlap the source string. Strncpy if the target length >=specify the length> the source length, the source string is copied to the target string, together with '\ 0' if the length is specified <The Source length, copy the truncated source string to the target string by the specified length, excluding '\ 0'. If the length is specified> the target length, the error is returned!

 

 

10. memcpy

Prototype: extern void * memcpy (void * DEST, void * SRC, unsigned int count); usage: # include <string. h> function: copy count bytes from the memory area indicated by Src to the memory area indicated by DeST. Note: the memory areas specified by Src and DEST cannot overlap. The function returns a pointer to DeST. Example: // memcpy. C # include <stdio. h> # include <string. h> int main (INT argc, char * argv []) {char * s = "golden Global View"; char d [20]; clrscr (); memcpy (D, s, strlen (s); D [strlen (s)] = '\ 0'; printf ("% s", d); getchar (); Return 0 ;} capture view # include <string. h> int main (INT argc, char * argv []) {char * s = "golden Global View"; char d [20]; memcpy (D, S + 14,4 ); // memcpy (D, S + 14 * sizeof (char), 4 * sizeof (char); or d [4] = '\ 0 '; printf ("% s", d); getchar (); Return 0;} output result: View initialization array char MSG [10]; memcpy (MSG, 0, sizeof (MSG ));

11. memset function: sets all the content of each byte in the memory to the ASCII value specified by Ch. The block size is specified by the third parameter, this function is usually used to initialize the newly applied memory: void memset (void * s, char CH, unsigned N); example: # include <string. h> # include <stdio. h> # include <memory. h> int main (void) {char buffer [] = "Hello world \ n"; printf ("buffer before memset: % s \ n", buffer); memset (buffer, '*', strlen (buffer); printf ("buffer after memset: % s \ n", buffer); Return 0;} output result: Buffer Before memset: Hello World buffer after memset: ************ compilation platform: microsoft Visual C ++ 6.0 does not necessarily set all content to the ASCII value specified by CH, And the CH here can be Int or other types, not necessarily char type. For example: int array [5] = {, 2}; For (INT I = 0; I <5; I ++) cout <array [I] <"; cout <Endl; memset (array, * sizeof (INT); For (int K = 0; k <5; k ++) cout <array [k] <"; cout <Endl; the output is: 1 4 3 5 2 0 0 0 0 after the table size parameter is in bytes, so for Int or other values, not all take the default 1 (Bytes type). In addition, the int size on different machines may be different, so it is best to use sizeof (). Note that the memset operation is performed on bytes, so if the above program is changed to int array [5] = {, 2}; For (INT I = 0; I <5; I ++) cout <array [I] <""; cout <Endl; memset (array, * sizeof (INT )); // note that this is different from the preceding program for (int K = 0; k <5; k ++) cout <array [k] <""; cout <Endl; the output result is: 1 4 3 5 2 16843009 16843009 16843009 16843009 16843009 why? Because memset is to assign values to the five bytes of memory pointed to by array in bytes, each of which is filled with characters with ASCII 1. After being converted to binary, 1 is 00000001, occupies one byte. An int element is 4 bytes, starting with 00000001000000010000000100000001, which is equal to 16843009. Then, the value of an int element is assigned. Therefore, using memset to assign initial values to non-struct arrays is not advisable! For example, if a struct of some X can be cleared like this: memset (& X, 0, sizeof (some); if it is an array of some X [10], you can do this: menset (x, 0, sizeof (some) * 10); [edit this section] memset function description 1. Void * memset (void * s, int C, size_t N): set the value of the first n Bytes of the memory space s to C. 2. Example main () {char * s = "golden Global View"; clrscr (); memset (S, 'G', 6 ); // It seems that there is a problem here // printf ("% s", S); getchar (); Return 0;} [this problem is quite large, and the program cannot run anymore, your s ambition here is a read-only memory, and you memset tries to modify it again, so an error occurs during the runtime. The modification method char * s is changed to Char s [] 3. The memset () function is often used for memory space initialization. For example, char STR [100]; memset (STR, 0,100); 4. Deep connotation of memset (): it is used to set all memory space to a specific character. It is generally used to initialize the defined string to 'memset (A, '\ 0 ', sizeof (a); memcpy is used for memory copying. You can use it to copy any data type object and specify the length of the copied data. For example, char a [100], B [50]; memcpy (B, A, sizeof (B); // note that if sizeof (a) is used, the memory address of B may overflow. Strcpy can only copy strings. It ends copying when '\ 0' is encountered. For example, char a [100], B [50]; strcpy (A, B ); for example, if strcpy (B, A) is used, check whether the length of the string in A (before the first '\ 0') exceeds 50 bits. If it exceeds, this will cause memory address overflow of B. 5. Supplement: some of your experiences with memset can easily clear a variable or array of the structure type. For example, struct sample_struct {char csname [16]; int iseq; int itype ;}; for the variable struct sample_strcut sttest; Generally, sttest is cleared. csname [0] = '\ 0'; sttest. iseq = 0; sttest. itype = 0; memset is very convenient: memset (& sttest, 0, sizeof (struct sample_struct); if it is an array: struct sample_struct test [10]; then memset (test, 0, sizeof (struct sample_struct) * 10); or memset (test, 0, sizeof (TEST ));

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.