08-C语言字符串

字符串

  • 将字符串到一起就是字符串,有序 / 同类型的 ---> 数组
  • 其实C语言中的字符串就是一个特殊的数组而已
  • 如果用数组表示字符串,那么数组必须是char类型,而且最后一个元素必须是\0
  • 在c语言中除了可以用字符数组来表示字符串以外,还可以用双引号" " 来表示字符串
以下就是一个字符串
char str1[] = {'c','w','w','\0'};

// 如果在定义的同时部分初始化,没有被赋值的元素会被自动赋值为0;
char str2[5] = {'c','w','w'};

char str3[] = "cww"; // 底层就是 {'c','w','w','\0'};

以下是一个字符数组,不是字符串
char str4[] = {'c','w','w'};

printf("str1 = %s\n",str1); // cww
printf("str2 = %s\n",str2); // cww
printf("str3 = %s\n",str3); // cww
printf("str4 = %s\n",str4); // cwwcww

image
  • 特别注意:字符串在内存中存储是连续的,只有遇到\0接结束
image

字符串常用方法

puts

  • 格式: puts(字符数组名)
  • 功能: 把字符数组中的字符串显示到控制台
  • 优点: 可以换行,可以是任意元素地址
```
char str1[] = "cww";
// printf + %s 输出不会自动换行,必须手动添加\n
printf("str = %s\n",str);
puts(str); // 会自动换行
printf("被换行");
```
![image](http://upload-images.jianshu.io/upload_images/7924287-73fd597a05f01451.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

gets

  • 格式:gets(字符数组名);
  • 功能:从键盘中输入一个字符串
```
char str2[10];
// scanf接收用户输入,
// scanf输入的数据中不能有空格/TAB/回车
scanf("%s", &str2);

//gets接收字符串,可以输入空格/TAB
// gets(str2);
printf("str2 = %s\n", str2);
```

strlen

  • 功能:计算字符串的长度
  • 利用sizeof计算字符串长度
```
char str1[] = "cww";
int len = sizeof(str1) / sizeof(str1[0]); 
// 注意这里计算是字符串占用内存单元的字节数
// 实际是len = len -1;
```

- 利用系统函数strlen计算
    - 注意: **在使用strlen要引入头文件 #include <string.h>;**
```
char str2[] = "cww";
strlen(str2);
```    
  • 自定义strlen函数
```
/**
 * @brief getStrlen  实现strlen函数
 * @param str        传入字符串
 * @return  len 长度
 */
int getStrlen (char str[]){
    int len = 0;
    while(str[len] != '\0'){
        len++;
    }
    return len;
}
```
  • 指针版
```
/**
 * @brief getStrlen  实现strlen函数
 * @param str        传入字符串
 * @return  len 长度
 */
int myStrlen(char *str){
    int index = 0;
    while(*str++){
        index++;
    }
    return index;
}
```
  • 注意点: *str++ 是先将地址加1在访问地址所存储的内容;
```
/**
 * @brief getStrlen  实现strlen函数
 * @param str        传入字符串
 * @return  len 长度
 */
int myStrlen(char *str){
    char *startP = str;
    while(*str++);
    str--;
    return str - startP;

}

```

strcat

  • 注意: 在使用strlen要引入头文件 #include <string.h>;
  • 格式:strcat(字符数组名1,字符数组名2);
  • 功能:把字符数组2中的字符串连接到字符数组1中字符串的后面,
  • 注意点:前面的数组必修足够长,否则在企业中会引发一些未知的BUG
```

char str1[10] = "cww";
char str2[4] = "ppp";
strcat(str1, str2);
printf("str1 = %s\n",str1);  // cwwppp

```
  • 自己封装的strcat函数
```

/**
 * @brief myStrcat  实现字符串的拼接
 * @param str1      传入字符串1
 * @param str2      传入字符串2
 */
void myStrcat(char *str1, char *str2){
    //printf("%s\n",str1);
    int index = 0;
    // 取出str1中存储的地址
    //printf("str1 = %p\n",str1);
    //printf("str1 = %c\n",*str1);
    while(*str1++) {
        index++;
    }
    //printf("str1 = %p\n",str1);
//    while (str1[index] != '\0') {
//        index++;
//    }
    printf("index = %i\n", index);
    int value = 0;
    while(str2[value] != '\0'){
        str1[index] = str2[value];
        index++;
        value++;
    }

    // printf("index = %i\n", index);
    str1[index] = '\0';
}
```
  • 指针版
```
/**
 * @brief myStrcat    指针优化的strcat函数
 * @param str1       传入字符串1
 * @param str2      传入字符串2
 */
void myStrcat(char *str1, char *str2){
    while(*str1++);
    str1--;
    while(*str2){
        *str1 = *str2;
        str1++;
        str2++;
    }
    *str1 = '\0';
}
```

strcpy

  • 注意: 在使用strlen要引入头文件 #include <string.h>;
  • 格式: strcpy(字符数组1,字符数组名2);
  • 功能: 把字符数组2中的字符串拷贝到字符数组1中.字符串结束标志'\0'也一同拷贝.
  • 注意点:前面的数组必修足够长,否则在企业中会引发一些未知的BUG
```
char str1[10] = "cww";
char str2[4] = "ppp";
strcpy(str1,str2);
printf("str1 = %s\n",str1); // ppp
printf("str2 = %s\n",str2); // ppp
```
  • 自定义strcpy函数
```
#include <stdio.h>
#include <string.h>
void myStrcpy(char *str1, char *str2);
int main()
{
    char str1[] = "cwww";
    char str2[] = "ppppp";
    //strcpy(str1, str2);
    myStrcpy(str1, str2);
    printf("str1 = %s\n", str1);
    return 0;
}

/**
 * @brief myStrcpy  实现strcpy函数
 * @param str1      传入字符串1
 * @param str2      传入字符串2
 */
void myStrcpy(char *str1, char *str2){
    int index = 0;
    while (str2[index] != '\0') {
        str1[index] = str2[index];
        index++;
    }
    str1[index] = '\0';
}

```
  • 指标版
```
/**
 * @brief myStrcpy  指针实现strcpy函数
 * @param str1      传入字符串1
 * @param str2      传入字符串2
 */
void myStrcpy(char *str1, char *str2){
    while(*str2){
        *str1 = *str2;
        str1++;
        str2++;
    }
    *str1 = '\0';
}
```

strcmp

  • 注意: 在使用strlen要引入头文件 #include <string.h>;
  • 格式: strcmp(字符数组名1,字符数组名2)
  • 功能: 按照ASCII码顺序比较两个数组中的字符串,并右函数返回值返回比较结果
    • 字符串1 = 字符串2, 返回值 = 0;
    • 字符串1 > 字符串2, 返回值 > 0;
    • 字符串1 < 字符串2, 返回值 < 0;
    • 如果前面的内容都相同, 第一个参数的个数小于第二个参数, 返回 -1 负数
    • 如果前面的内容都相同, 第一个参数的个数大于第二个参数, 返回 1 正数
```
char str1[10] = "124";
char str2[4] = "123";
int res = strcmp(str1,str2);
printf("res = %i\n", res);  // 1
```
  • 自定义strcmp
```
#include <stdio.h>
#include <string.h>
int myStrcmp(char *str1, char *str2);
int main()
{
    char str1[] = "1224";
    char str2[] = "12233";
    int res1 = strcmp(str1, str2);
    printf("res1 = %i\n", res1);

    int res2 = myStrcmp(str1, str2);
    printf("res2 = %i\n", res2);
    return 0;
}

/**
 * @brief myStrcmp  实现strcmp函数
 * @param str1      传入第一个字符串
 * @param str2      传入第二个字符串
 * @return 
 */
int myStrcmp(char *str1, char *str2){
    int index = 0;
    while (str1[index] != '\0' || str2[index] != '\0') {
        if(str1[index] > str2[index]){
            return 1;
        }else if(str1[index] < str2[index]){
            return -1;
        }
        index++;
    }
    return 0;
}

```
  • 指针版
```

/**
 * @brief myStrcmp  指针实现strcmp函数
 * @param str1      传入第一个字符串
 * @param str2      传入第二个字符串
 * @return
 */
int myStrcmp(char *str1, char *str2){
    while (*str1 || *str2) {
        if(*str1 > *str2){
            return 1;
        }else if(*str1 < *str2){
            return -1;
        }
        str1++;
        str2++;
    }
    return 0;
}
```

字符串数组

  • 字符串数组其实就是定义一个数组保存所有的字符串
char names1[2][6] = {{'c','w','w','\0'},{'p','p','p'}};
char names2[2][6] = {{"cww"},{"ppp"}};
char names3[2][6] = {"cww","ppp"};

  • 指针定义字符串
char *str1 = "cww";
char *str2 = "abc";
char *str3 = "def";
char *str{
    str1,
    str2,
    str3
};

// 字符串数组的第二种格式
char *str[] = {
    "cww",
    "abc",
    "def"
};
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容