IOS learning notes (string operations)

Source: Internet
Author: User
Tags strtok

IOS learning notes (string operations)
I. String operations 1) storage of strings, character arrays, and applying for memory space in the heap. Instance code:

# Include <stdlib. h> # include <string. h> int main () {char str [100] = "hello world"; char * p = "hello world"; // * p = 'H '; // The constant string cannot be modified. p points to the read-only address p = str; * p = 'H'; // printf ("% s", str ); p = (char *) malloc (100); // empty if (p = NULL) return 0; // reset memset (p, 0,100 ); printf ("p = % p \ n", p); // string assignment // memcpy (p, "helloworld", 10); // Method 1: sprintf (p, "% s", "helloworld"); // Method 2: sprintf () Output Value assignment // p = "helloworld"; // error, p points to the constant area, and the applied memory is lost printf ("p = % p \ n", p); printf ("% s", p ); // release free (p); return 0 ;}

 

2) character operation function. The parameter is the ASCII code value of the corresponding parameter. If the condition is met, 1 is returned. If the condition is not met, 0 is returned. The header file must be referenced. # include <ctype. h> 1. int isalnum (int); // determines whether it is a number or letter 2 or int isalpha (int). // determines whether it is an English character 3 or int isdigit (int ); // determine whether the number is 0 ~ 9 4. int islower (int); // determines whether it is a lowercase letter 5 or int isupper (int); // determines whether it is a capital letter 6 or int isxdigit (int ); // whether it is a hexadecimal number 7 or int tolower (int); // convert it to lowercase 8 or int toupper (int); // convert it to uppercase 9 or int digittoint (int ); // convert hexadecimal numeric characters to integer 10, int ishexnumber (int); // equivalent isxdigit 11, int isnumber (int); // equivalent isdigit instance code:
 1 #include <ctype.h> 2 int main() 3 { 4     printf("%d\n",isalnum('1'));//1 5     printf("%d\n",isalpha('1'));//0 6     printf("%d\n",isdigit('a'));//0 7     printf("%d\n",islower('A'));//0 8     printf("%d\n",isupper('a'));//0 9     printf("%d\n",isxdigit('F')); //110     printf("%c\n",tolower('A'));//a11     printf("%c\n",toupper('a'));//A12     printf("%d\n",digittoint('1'));//113     printf("%d\n",ishexnumber('a'));//114     printf("%d\n",isnumber('a'));//015     return 0;16 }

 

3) Use strcpy or strncpy to copy strings. You can specify the copy length. Instance code:
1 int main () 2 {3 char str [20] = "XXXXXXXXXXXXXX"; 4 char * p = "hello"; 5 strcpy (str, p ); // copy the string from the position of p and copy the '\ 0' following p. if the received string is not large enough, it may cross the border. 6 printf ("% s \ n", str); // print from str to \ 0 end 7 for (int I = 0; I <10; I ++) {8 printf ("str [% d] = % c = % d \ n", I, str [I], str [I]); 9} 10 unsigned long len = strlen (str); 11 strncpy (str, p, len-1); // The final parameter: Max number of copy characters. typically set as the destination string bufsize-1, reserved Terminator 12 str [len-1] = '\ 0 '; // set the last element of the array to the ending character 13 printf ("% s \ n", str); // print from str to \ 0 end 14 15}

 

4) string comparison function strcmp. The latter of strncmp can limit the length of the comparison string. If the comparison result is equal to 0, the result of s1> s2 is greater than 0; if the result of s1 <s2 is less than 0; the comparison result is equal to the difference of the ascii code of the first non-equal character in the string. Instance code:
1 int main () 2 {3 char * p1 = "helloAworld"; 4 char * p2 = "helloB"; 5 int rst; 6 rst = strcmp (p1, p2 ); 7 printf ("rst = % d \ n", rst); // result:-1 8 rst = strncmp (p1, p2, 2 ); // 2 indicates the maximum length of the comparison 9 printf ("rst = % d \ n", rst); // result: 010 return 0; 11}

 

5) Search for string functions 1. strchr searches from left to right. 2. Reverse query of strrchr from right to left. 3. strstr returns the position where the s2 string appears for the first time in the s1 string. If the corresponding string is not found, NULL is returned. Instance code:
1 int main () 2 {3 char * p = "hello china world"; 4 char * pRst; 5 pRst = strchr (p, 'x '); // return NULL 6 printf ("% s \ n", pRst); 7 printf ("% s \ n", strchr (p, 'L') if not found ')); // forward 8 printf ("% s \ n", strrchr (p, 'L'); // reverse 9 pRst = strstr (p, "china "); 10 printf ("% s \ n", pRst); 11 return 0; 12}

 

6) String concatenation functions 1. strcat: The structure char * strcat (char * dst, const char * src) the remaining memory space pointed to by dst should be sufficient to accommodate the src character string, the memory to which dst src points cannot overlap. 2. strncat, structure char * strncat (char *, const char *, size_t n) size_t n: maximum number of Concatenated characters // array length-current valid number-1 (Reserved Terminator) instance code:
1 int main () 2 {3 char str [20] = "hello"; 4 strcat (str, "world"); // At the end of the original string, add the new string 5 printf ("% s \ n", str); 6 int len = (int) strlen (str); 7 strncat (str, "hello world ", 20-len-1); 8 len = (int) strlen (str); 9 printf ("strlen = % d, % s \ n", len, str); 10 return 0; 11}

 

7) string split function strtok structure char * strtok (char * src, const char * demi ). 1. strtok converts all separators in the src string to '\ 0' 2. Directly modifies the original string 3. The first address of the string must be passed only when the first call is made, from the second start, NULL is required. 4. Write a function to intercept a string and extract any part of the characters to be truncated. The Code is as follows:
1 void * splitstr (char * str, char * sp, int index) {2 char * result = NULL; 3 result = strtok (str, sp); 4 int num = 0; 5 while (result! = NULL) {6 if (num = index) 7 break; 8 result = strtok (NULL, sp); 9 num ++; 10} 11 return result; 12} 13 14 int main () {15 char str [] = "I love # China # People # Republic"; 16 char sp [] = "#"; 17 char * spstr = str; 18 char * spf = sp; 19 char * result = (char *) splitstr (spstr, spf, 2); // result: people 20 printf ("% s", result); 21 return 0; 22}

 

8) search string 1 and strpbrk functions. The structure is char * strpbrk (const char * s1, const char * s2). The characters in s1 are retrieved in sequence. When s2 also contains, stop the search and return the location. 2. strspn function; the structure is size_t strspn (const char * s1, const char * s2); // example code of the number of consecutive characters starting with s1 in S2:
1 int main () 2 {3 char * p1 = "hello world"; 4 char * p2 = "girl"; 5 printf ("% s \ n", strpbrk (p1, p2); // 'L' is also available in p2. 'L' is the first common character of two strings, returns the address 6 char * p3 = "lehRwsrt"; 7 printf ("% lu \ n", strspn (p1, p3) in p1; // p1, the first four characters, which also have 8 return 0; 9} In p3}

 

9) extract integers starting with a number and ending with a letter. There are 1 implementation functions. atoi is used to extract int data. 2. atof is used to extract double data, float and other floating point data 3. atol is used to extract long integer data such as long data instance code:
 1 int main(){ 2     int a; 3     a=atoi("2adse"); 4     printf("a=%d\n",a); 5  6     double d; 7     d=atof("122dsf3.34f4"); 8     printf("d=%.3f\n",d); 9 10     long l;11     l=atol("23454df3556s");12     printf("l=%ld\n",l);13     return 0;14 }

 

10) reverse string instance code:
1 char * reverse (char * s) {2 char ch; 3 unsigned long len = strlen (s); 4 for (int I = 0; I <= len/2; I ++) {5 ch = * (s + I); 6 * (s + I) = * (s + len-1-i); 7 * (s + len-1-i) = ch; 8} 9 return s; 10} 11 12 char * strcat1 (char * s, const char * ct) {13 char * p = s; 14 while (* p! = '\ 0') {15 p ++; 16} 17 18 while (* ct! = '\ 0') {19 * p = * ct; 20 p ++; 21 ct ++; 22} 23 return s; 24} 25 26 int main () {27 // reverse string 28 char str [100] = "asdfer"; 29 printf ("% s", reverse (str); 30 char * qstr = "qianfeng "; 31 strcat1 (str, qstr); 32 printf ("% s", str); 33 return 0; 34}

 

 

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.