C language | string manipulation

1. Matters needing attention

When using strings in C language, use #include <string.h>

Use #include <string> in C++

Two, string operation

1. Copy all strings: char* strcpy(char* dest,const char* src);

Copy the string pointed to by src to the space pointed to by dest, and'\0' will also be copied.

    #include <string.h>

    char dest_str[100] = { 0 };
    const char* str = "hello\0 world";
    strcpy(dest_str, str);
    printf("%s\n", dest_str);//hello

2. String copy n: char* strncpy(char* dest,const char* src,size_t n);

Copy the first n characters of the string pointed to by src to the space pointed to by dest. If it contains'\0', it will also be copied.

    #include <stdio.h>

    char str[] =  "10,hello" ; 
    int a=0;
    char new_str[100] = {0};
    sscanf(str, "%d,%s", &a, new_str);
    printf("%d\n", a);//10
    printf("%s\n", new_str);//hello

3. String connection: char* strcat(char* dest,const char* src);

Connect the string pointed to by src to the end of the space pointed to by dest, and'\0' will also be appended to return the first address of the dest string.

    #include <string.h>

    char dest_str[100] = "My First Prigram:";
    char* str = "hello\0 world";
    strcat(dest_str, str);
    printf("%s\n", dest_str);//My First Prigram:hello

4. String comparison: int strcmp(const char* s1,const char* s2);

Compare the ASCII size of the characters in s1 and s2, and return 0 if they are equal.

    #include <string.h>

    char dest_str[100] =  "hello";
    char* str = "hello\0 world";
    int ret = strcmp(dest_str, str);
    printf("%d\n", ret);//0,相同

5. String comparison: int strncmp(const char* s1,const char* s2,size_t n);

Compare the ASCII size of the first n characters of s1 and s2, and return 0 if they are equal.

    #include <string.h>

    char dest_str[100] =  "hello world" ;
    char* str = "hello WORLD";
    int ret = strncmp(dest_str, str,6);
    printf("%d\n", ret);//0,相同

6. String formatting: int sprintf(char* str,const char* format,...);

Convert and format the data according to the parameter format string, and output the result to the str space until the end of the string'\0' appears.

    #include <stdio.h>

    char str[] =  "hello" ;
    int a = 10;
    char new_str[100];
    sprintf(new_str, "%d %s",a,str);
    printf("%s\n", new_str);//10 hello

7. String formatting: int sscanf(char* str,const char* format,...);

Read data from the specified str, convert and format the data according to the parameter format string.

    #include <string.h>

    char dest_str[100] =  "hello";
    char* str = "hello\0 world";
    int ret = strcmp(dest_str, str);
    printf("%d\n", ret);//0,相同

8. Find the position where the character appears: char* strchr(const char *s,int c);

Find the position where the character c appears in the string s.

    #include <string.h>

    char str[] = "hello world";
    char* new_str = strchr(str, 'l');
    printf("%s\n", new_str);//hello

9. Find the position where the string appears: char* strstr(const char *str,const char *s);

Find the position where the string s appears in the string str.

    #include <string.h>

    char str[] = { "hello world" };
    char* new_str = strstr(str,"o");
    printf("%s\n", new_str);//hello

10. Cutting string: char* strtok(char *str,const char *s);

When the string s is found in the string str, change s to \0 character. In the first call, the str parameter must be assigned a value; subsequent calls will set str to NULL. strtok() will destroy the source string.

    #include <string.h>

    char str[] = "hello,world,ok,fine";
    char* new_str = strtok(str, ",");
    while (new_str)
    {
        printf("%s\n", new_str);
        new_str=strtok(NULL,",");
    }

11. String to number: int atoi(const char *s);

Convert the string s to an integer. When atoi scans s, it skips the preceding empty string, and does not start conversion until it encounters a number or sign, and ends the conversion when it encounters a non-digit or \0.

    #include <stdlib.h>

    char str[] = " 123.5ab" ;
    int num = atoi(str);
    printf("%d\n", num);

    double d = atof(str);
    printf("%f\n", d);

 

Guess you like

Origin blog.csdn.net/weixin_39766005/article/details/108760887