character string
It is essentially a character array
Definition method:
#include <stdio.h> int main() { //The first way to define a string is analogous to an array of integers char str[5] = {'a','b','c','d','e'}; for(int i=0;i<sizeof(str)/sizeof(str[0]);i++){ printf("%c",str[i]); } putchar('\n'); //The second way char str2[5] = "abcde"; for(int i=0;i<sizeof(str2)/sizeof(str2[0]);i++){ printf("%c",str2[i]); } putchar('\n'); //The third way char str3[] = "abcdefgh"; for(int i=0;i<sizeof(str3)/sizeof(str3[0]);i++){ printf("%c",str3[i]); } putchar('\n'); //In the fourth way, the array name is the address (in most cases) char *pstr = "hello world"; //If the pointer is not operated properly, it is easy to cause segment errors printf("%s\n",pstr);//The string is represented by the format placeholder% s and does not need to be traversed by the subscript of i return 0; }
String storage method:
#include <stdio.h> int main() //String in memory, in addition to valid characters, it will be automatically followed by '\ 0' as the end flag of the string { int a[] = {1,2,3}; //Integer array, continuous space. The size of the whole array is the number of elements * the size of element type printf("a The size of the array is:%d\n",sizeof(a)); printf("The element size of the array is:%d\n",sizeof(a[0])); printf("Number of arrays:%d\n",sizeof(a)/sizeof(a[0])); char a2[3] = {'a','b','c'}; printf("a2 The size of the array is:%d\n",sizeof(a2)); printf("The element size of the array is:%d\n",sizeof(a2[0])); printf("Number of arrays:%d\n",sizeof(a2)/sizeof(a2[0])); char a3[3] = "abc"; printf("a3 The size of the array is:%d\n",sizeof(a3)); printf("The element size of the array is:%d\n",sizeof(a3[0])); printf("Number of arrays:%d\n",sizeof(a3)/sizeof(a3[0])); char a4[] = "abc"; //Without writing the length of the array, the size of the array will be determined according to the number of elements during initialization printf("a4 The size of the array is:%d\n",sizeof(a4)); printf("The element size of the array is:%d\n",sizeof(a4[0])); printf("Number of arrays:%d\n",sizeof(a4)/sizeof(a4[0])); //The result is 4, one more character, because '\ 0' represents the end flag of the string int i = 0; while(a4[i] != '\0'){ printf("%c",a4[i]); i++; } return 0; }
Differences between sizeof and strlen in string calculation:
#include <stdio.h> #include <string.h> //sizeof cannot be used to count the number of valid characters in a string //strlen should be used. When calculating the string size, it ends counting after encountering '\ 0' int main() { int a[] = {1,2,3}; printf("The number of array elements is:%d\n",sizeof(a)/sizeof(a[0])); char a2[128] = "hello"; printf("The number of array elements is:%d\n",sizeof(a2)/sizeof(a2[0])); printf("Number of valid elements in the array:%d\n",strlen(a2)); return 0; }
Several common API s for string:
1.puts -- output string, similar to printf
#include <stdio.h> #include <string.h> int main() { char *str = "hello world"; puts(str); printf("%s\n",str); return 0; }
2.gets/scanf -- get string
char str[128] = {'\0'}; 128byte is applied in memory, Initialize to '\ 0'
char *pstr: defines a pointer to store other addresses. It is a wild pointer
#include <stdio.h> #include <string.h> int main() { // char *pstr; Wild pointer, causing illegal memory access, and segment error will occur //char pstr[128] = {'\0'}; Feasible first method char *pstr = null; pstr = (char *)malloc(128); //The second method is to open up space and whether the memory is legal printf("Please enter a string:\n"); scanf("%s",pstr); puts(pstr); return 0; }
3.strlen / sizeof - calculated length
4.memset - initialization
#include <stdio.h> #include <string.h> int main() { // char *pstr; Wild pointer, causing illegal memory access, and segment error will occur //char pstr[128] = {'\0'}; Apply for space; Initialization, initialize each element as' \ 0 ' char *pstr = NULL; pstr = (char *)malloc(128); //1. Apply for space 2. Once malloc is used, be sure to pay attention to memory leakage 3.malloc may fail. Judge the return value if(pstr == NULL){ printf("Failed to request memory\n"); exit(-1); } memset(pstr,'/0',128); //2. Initialize each item to '\ 0' //The memset function has three parameters: initialization object; What characters are initialized to; How much space printf("Please enter a string:\n"); scanf("%s",pstr); puts(pstr); return 0; }
5.strcpy / strncpy - copy
Strcpy prototype declaration: char *strcpy(char* dest, const char *src);
Function: put src Copy the string pointed to to to dest.
It should be noted that if the destination array dest is not large enough and the length of the source string is too long, a buffer overflow may occur.
#include <stdio.h> #include <string.h> int main() { char src[40]; char dest[100]; memset(dest,'\0',sizeof(dest)); strcpy(src,"hello"); strcpy(dest,src); printf("Final string:%s\n",dest); return 0; }
Strncpy prototype declaration: char *strncpy(char *dest, const char *src, size_t n)
Function: put src Copy the string pointed to to to dest, copy at most n Characters. When the length of SRC is less than N, the rest of dest will be filled with empty bytes.
There are three parameters:
dest -- Points to the target array used to store the copied content.
src -- String to copy.
n -- The number of characters to copy from the source
#include <stdio.h> #include <string.h> int main() { char src[40]; char dest[100]; memset(dest,'\0',sizeof(dest)); strcpy(src,"syzdsb"); strncpy(dest,src,6); printf("Final string:%s\n",dest); return 0; }
6.strcat - splicing
Declaration of strcat() function: char *strcat(char *dest, const char *src)
Function: put src The string pointed to is appended to dest The end of the string pointed to.
There are two parameters:
dest: point to the target array, which contains a C string and is enough to hold the appended string;
src: points to the string to append, which does not overwrite the target character.
#include <stdio.h> #include <string.h> int main() { char src[40]; char dest[40]; strcpy(dest,"study hard"); strcpy(src,"make progress every day"); strcat(dest,src); printf("Final string:%s\n",dest); return 0; }
7.strmp -- string comparison
Declaration of strcmp() function: int strcmp(const char *str1, const char *str2)
There are two parameters:
str1: the first string to compare;
str2: the second string to compare
Return value:
If STR1 < STR2, the return value is less than 0;
If str1=str2, the return value is equal to 0;
If STR1 > STR2, the return value is greater than 0;
#include <stdio.h> #include <string.h> int main() { char *str1 = "abc"; //Is compared by ASCII value size char *str2 = "abcd"; int ret = strcmp(str1,str2); printf("%d\n",ret); return 0; }
8.strchr -- string retrieval
Declaration of strchr() function: char *strchr(const char *str, int c)
Role: in parameters str Search for the first character in the string pointed to Position of c (an unsigned character)
There are two parameters:
str: the c string to be retrieved
c: Characters to search in str
Return value: this function returns the pointer of the character c for the first time in the string str. if the character is not found, it returns NULL
#include <stdio.h> #include <string.h> int main() { char *str = "qieerbushe"; char c = 'e'; char *p = NULL; p = strchr(str,c); puts(p); return 0; }
9. STR -- find substring
Declaration of strstr() function: char * strstr (const char * haystack, const char * need)
Function: in string haystack Find the first occurrence string in needle The position of does not contain the terminator '\ 0'
Two parameters:
haystack -- The C string to be retrieved;
needle -- The small string to search within the haystack string.
Return value: this function returns the position where the need string appears for the first time in haystack. If it is not found, it returns null
#include <stdio.h> #include <string.h> int main() { char *str = "qieerbushe"; char *substr = "bu"; char *p = NULL; p = strstr(str,substr); puts(p); return 0; }
10.strlwr -- convert to lowercase
Declaration of strlwr() function: char *strlwr(char *str);
Function: converts characters in a string to lowercase
Parameter: str: string to convert
Return value: returns the converted lowercase string, that is, str is returned
#include <stdio.h> #include <string.h> int main() { char str[] = "QIEERBUSHE"; //It cannot be defined as char *str = "" in Windows environment and will crash, but it can be defined in Linux puts(strlwr(str)); return 0; }
11.strupr -- convert to uppercase
Declaration of strupr() function: char *strupr(char *str)
Function: converts characters in a string to uppercase
Parameter: str: string to convert
Return value: returns the converted uppercase string
#include <stdio.h> #include <string.h> int main() { char str[] = "qieerbushe"; puts(strupr(str)); return 0; }
12.strtok -- string segmentation
Declaration of strtok() function: char *strtok(char *str, const char *delim)
Function: decompose string str Is a set of strings, delim Is a separator
Parameters:
str -- A string to be decomposed into a set of small strings;
delim -- C string containing separator;
Return value: this function returns the first substring decomposed. If there is no retrievable string, it returns a null pointer.
#include <stdio.h> #include <string.h> int main() { char str[] = "qie,er,bu,she"; char *p = NULL; /*p = strtok(str,","); printf("Get the first string p=%s\n",p); p = strtok(NULL,","); printf("Get the second string p=%s\n",p); The second start target string is changed to NULL*/ p = strtok(str,","); while( p != NULL ){ printf("%s\n",p); p = strtok(NULL,","); } return 0; }