There are two kinds of character strings for C:

A character array

An array can modify one of its values, not the whole array.

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>

// Use character arrays to store strings
void  main(a) {

    // There are three ways to write it
    //\0 represents the end character
    char str[] = { 'h'.'e'.'l'.'l'.'o'.'\ 0' };
    //char str[6]= { 'h','e','l','l','o' };
    //char str[10] = "hello";

    printf("%s\n", str);
    / / address
    printf("%#x\n", str);
    getchar(a); }Copy the code

Result output:

hello
0xb3fb78
Copy the code

A character pointer

A character pointer cannot change any of its values, but can be assigned as a whole. Using pointer addition, in conjunction with the terminator, interception is possible.

void main(a) {
    char *str = "how are you?";
    printf("%s\n", str);
    //str[1] = "w" ; // The character pointer cannot change one of its values

    str = "hello world";
    printf("%s\n", str);
    printf("%#x\n", str);

    // Use pointer addition to intercept strings
    str += 3; // point to the beginning of the fourth character
    while (*str) {
        printf("%c", *str);
        str++;

    }
    getchar(a); }Copy the code

Result output:

How are you? hello world 0x97b44 lo worldCopy the code

A common method for strings

Online manual: www.kuqin.com/clib/ Related header files: #include

Strcpy String concatenation

Extern char *strcpy(char *dest,char * SRC);

Function: Copy the null-terminated SRC string to the array indicated by dest.

Note: SRC and dest must not overlap and dest must have enough space to hold the SRC string. Returns a pointer to dest.

For example:


void main(void){
    char dest[50];  
    char *a = "china";
    char *b = " is powerful!";
    strcpy(dest, a);
    strcat(dest, b);
    printf("%s\n", dest);

    system("pause");
}
Copy the code

Result output:

china is powerful!
Copy the code

STRCHR Searches for characters in a string

Extern char * STRCHR (char *s,char C);

Find the first occurrence of character C in string s

Description: Returns a pointer to the first occurrence of C, or NULL if c is not present in S.


void main(void){
    char *str = "I want go to USA!";
    printf("%#x\n", str);
    // The pointer to the U element
    //str+3
    char* p = strchr(str, 'w');
    if (p){
        printf("Index location: %d\n", p - str);
    }
    else{
        printf("Not found.");
    }

    system("pause");
}
Copy the code

Result output:

0x877B30 Index Location: 2Copy the code

STRCHR Searches for characters in a string

Extern char * STRSTR (char *haystack, char *needle);

Function: Find the first occurrence of needle in the string haystack (without comparing the terminator NULL).

Description: Returns a pointer to the position where a needle was first seen, or NULL if no needle was found.


// STRSTR looks for the position where needle first appeared in the string haystack

void main(void){
    char *haystack = "I want go to USA!";
    char *needle = "to";
    // The pointer to the U element

    char* p = strstr(haystack, needle);
    if (p){
        printf("Index location: %d\n", p - haystack);
    }
    else{
        printf("Not found.");
    }

    system("pause");
}
Copy the code

Result output:

Index position: 10Copy the code

More usage…


//strset sets all characters in string s to character C
void main(void){
    char str[] = "internet change the world!";
    _strset(str,'w');
    printf("%s\n",str);
    system("pause");
}

//strrev reverses the order of all characters in the string s
void main(void){
    char str[] = "internet change the world!";
    _strrev(str);
    printf("%s\n", str);
    system("pause");
}

// Atoi string is converted to int
//atol() : converts a string to a long integer value
void main(void){
    char* str = "a78";
    //int r = atoi(str);    
    printf("%d\n", r);

    system("pause");
}

// The string is converted to double
void main(void){
    char* str = "77b8b";
    char** p = NULL;
    //char* p = str + 2;
    // Parameter description: STR is the string to be converted, and endstr is the pointer to the first unconverted character
    double r = strtod(str,p);
    printf("%lf\n", r);
    printf("%#x\n", p);

    system("pause");
}

//strupr converts to uppercase
void main(void){
    char str[] = "CHINA motherland!";
    _strupr(str);
    printf("%s\n",str);
    system("pause");
}

// Convert to lowercase
void mystrlwr(char str[],int len){
    int i = 0;
    for (; i < len; i++){
        / / a-z a-z letters
        if (str[i] >= 'A' && str[i] <= 'Z'){
            str[i] = str[i]-'A' + 'a'; }}}void main(void){
    char str[] = "CHINA motherland!";
    mystrlwr(str,strlen(str));
    printf("%s\n", str);
    system("pause");
}

// Exercise: Delete the specified character in the string
void delchar(char *str, char del){
    char *p = str;
    while(*str ! ='\ 0') {
        if(*str ! = del) { *p++ = *str; } str++; } *p ='\ 0';
}


// Delete the last character
int main(a)
{
    char str[] = "vencent ppqq";

    delchar(str,'t');
    printf("%s\n", str);
    
    system("pause");
}

//Java String replaceAll 
//StringBuffer buff.deleteCharAt(buff.length()-1);
// Delete the last character
void main(void){
    char str[] = "internet,";
    str[strlen(str) - 1] = '\ 0';
    printf("%s\n", str);
        
    // Job: Realloc implements the concatenation of StringBuffers rather than opening up a large array at first
    // Structure StringBuffer

    system("pause");
}

//memcpy copies count bytes from SRC to dest
void main(void){
    char src[] = "C,C++,Java";
    char dest[20] = {0};

    / / byte
    memcpy(dest,src,5);
    
    printf("%s\n",dest);
    system("pause");
}

//memchr looks for the character ch from the first count bytes of the memory region referred to by buf.
void main(void){
    char src[] = "C,C++,Java";
    char ch = 'C';

    // bytes (segmented)
    char* p = memchr(src+3, ch, 5);
    if (p){
        printf("Index: %d\n", p - src);
    }
    else{
        printf("Can't find \n");
    }

    
    system("pause");
}

//memmove copies count bytes from SRC to dest.
void main(a){
    char s[] = "Michael Jackson!";
    // Capture the effect
    memmove(s, s + 8.strlen(s) - 8 - 1);
    s[strlen(s) - 8] = 0;
    printf("%s\n", s);
    getchar(a); }// Find the position of the first character matching any character in string s2 in string s1, excluding the NULL character
void main(a){
    char *s1 = "Welcome To Beijing";
    char *s2 = "to"; 
    char *p;

    p = strpbrk(s1, s2);
    if (p)
        printf("%s\n", p);
    else
        printf("Not Found! \n");

    p = strpbrk(s1, "Da");
    if (p)
        printf("%s", p);
    else
        printf("Not Found!");

    getchar(a); }Copy the code