[Key points of C language (2)] Little things about strings

[Key points of C language (2)] Little things about strings

1. Distinguish three ways of string declaration

(1)char str[ ] = {‘h’,‘e’,‘l’,‘l’,‘o’};
(2)char *str = “hello”;
(3)char str[ ] = “hello”;
  • Definition method: (1) character array; (2) pointer to string constant; (3) string literal (string constant)

    (2)仅声明了一个指针变量指向字符串hello,而该字符串存放在常量区,不允许被修改

  • A small interview question: (D)
    Insert image description here

  • The following way of writing is the correct way to open the string to modify it
    Insert image description here

不同于上面的char *str = "Hello";char str[ ]=“Hello world"这种声明编译器会给数组在栈中分配内存空间,并将字符串存入数组的可修改地址中

2. Things you must remember about strings

  • End mark: '\0': Remember to add 1 to the sizeof() value; strlen() calculates the effective character length of the string, and '\0' is ignored.
  • Dynamically open strings: malloc(), realloc(), free().记得回收资源
  • Commonly used string manipulation functions: ① copy strcpy( )\strncpy( ); ② splice strcat( ); ③ compare strcmp( );

3. Common interview questions

(1) Implement the string copy function yourself
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

char *myStrcpy(char *dest,const char *src){
    
    //const修饰,src不可变(加分)
    char *temp = dest;  //保存目标字符串,作为返回值,使函数可以作为其他函数的参数传入(加分)
    if(src == NULL || dest == NULL){
    
        //NULL判断(加分)
        return NULL;            //写法有很多,选择容易理解的即可
    }
    while (*src != '\0'){
    
    
        *dest++ = *src++;
    }
    *dest = '\0';   //加上结束标志
    return temp;
}

char *myStrncpy(char *dest,const char *src,size_t size){
    
    //const修饰,src不可变(加分)
    char *temp = dest;  //保存目标字符串,作为返回值,使函数可以作为其他函数的参数传入(加分)
    if(src == NULL || dest == NULL){
    
        //NULL判断(加分)
        return NULL;            //写法有很多,选择容易理解的即可
    }
    while (*src != '\0' && size-- > 0){
    
    
        *dest++ = *src++;
    }
    *dest = '\0';   //加上结束标志
    return temp;
}

int main(){
    
    
    char *str1 = "myStringCopy";
    char *str2 = (char *)malloc(strlen(str1));
    char *str3 = (char *)malloc(strlen(str1));
    myStrcpy(str2,str1);
    printf("%s\n",str2);
    printf("%s\n",myStrcpy(str2,str1)); //返回值的意义

    myStrncpy(str3,str1,8);
    puts(str3);
    return 0;
}

Insert image description here

  • C language assertion function assert(): Its function is to calculate the expression. If it is false, it will print an error message to the standard error stderr and call abort to terminate the program. The disadvantage of assertion is that frequent calls cause additional overhead and affect performance.
    Insert image description here
(2) Implement the character splicing function yourself
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>

char *myStrcat(char *dest,const char *src){
    
    
    char *temp = dest;
    assert(dest != NULL && src != NULL ); //断言
    char *destAppendPos = dest + strlen(dest);   //末尾地址
    while (*src != '\0'){
    
    
        *destAppendPos++ = *src++;
    }
    *destAppendPos = '\0';
    return temp;
}

int main(){
    
    
    char str1[] = "String1";
    char str2[] = "String2";
    myStrcat(str1,str2);
    printf("%s\n",str1);
    return 0;
}
(3) Implement the string comparison function yourself (omitted)

Insert image description here

Guess you like

Origin blog.csdn.net/weixin_54429787/article/details/129429753