C语言菜鸟教程:字符数组和字符串

2024-05-0210:43:38编程语言入门到精通Comments269 views字数 2498阅读模式

基本使用

示例代码:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/63402.html

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>

int main() {
 char arr[3] = { 'a','b','c' };
 int size = sizeof(arr) / sizeof(arr[0]);

 for (int i=0;i<size;i++)
 {
  printf("%c ", arr[i]);
 }
 printf("\n");

 return 0;
}

输出:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/63402.html

a b c

字符串是字符数组

如果字符数组中含有'\0',那么这个字符数组就是一个字符串。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/63402.html

示例代码:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/63402.html

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>

int main() {
 // 这可以当成是字符串
 char arr[4] = { 'a','b','c','\0' };

 // 直接打印字符串
 printf("%s\n", arr);

 return 0;
}

输出:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/63402.html

abc

字符数组可以存字符串

示例代码:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/63402.html

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>

int main() {
 // 字符数组可以直接存字符串
 // char arr[4] = { 'a','b','c','\0' };

 // 和上面的表达式等价
 char arr[4] = "abc";

 // 直接打印字符串
 printf("%s\n", arr);

 return 0;
}

输出:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/63402.html

abc

scanf录入字符串

注意,下面的这种方式容易造成内存污染。比如开辟的是3个字符空间,但是输入的字符超过了3个,就会造成内存污染,因为输入会占用未开辟的空间。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/63402.html

示例代码:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/63402.html

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>

int main() {
 // 定义字符数组,实际上是个字符串
 // 这里的128也可以是其他数字
 char num[128] = "";

 // 录入字符串
 printf("请输入:");

 // 注意:这里的num没有加&,因为scanf需要的是变量的首元素地址
 scanf("%s", num);

 // 输出
 printf("您输入的是:%s\n", num);

 return 0;
}

输出:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/63402.html

请输入:abcdefg
您输入的是:abcdefg

fgets录入字符串

这个方法相对来说比较安全,因为第二个参数可以指定最大可以读多少个字符。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/63402.html

这个方法会把最后结束的\n也读取进来。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/63402.html

示例代码:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/63402.html

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>

int main() {
 // 定义字符数组,实际上是个字符串
 // 这里的128也可以是其他数字
 char num[128] = "";

 // 录入字符串
 printf("请输入:");

 // 注意:这里的num没有加&,因为scanf需要的是变量的首元素地址
 fgets(num,sizeof(num), stdin);

 // 输出
 printf("您输入的是:%s\n", num);

 return 0;
}

输出:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/63402.html

请输入:abc
您输入的是:abc

字符数组拼接

这是一个非常综合的知识点,涉及到的东西比较多,主要如下:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/63402.html

  • 字符串本质上是一个字符数组,这个字符数组以\0结束
  • 字符数组的名称本质上是这个字符数组在内存中的首地址
  • 指针可以做加减法运算

示例代码:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/63402.html

#include <stdio.h>
#include <string.h>

// 字符数组拼接
void myStrcat(char *str1, char *str2) {
    // str1的长度
    int n = strlen(str1);

    // str2中的遍历序号
    int i = 0;

    // 当str2中还没有遍历完的时候,一直遍历
    while (*(str2 + i) != 0) {
        // 将str2中的值依次追加到str1后面
        *(str1 + n + i) = *(str2 + i);
        i++;
    }

    // 最后,在str1的末尾补0,表示这是一个字符串,已经结束
    *(str1 + n + i) = 0;
}

int main(void) {
    // 定义两个字符数组
    char str1[128] = "hello";
    char str2[128] = "123456";

    // 输出
    printf("%s\n", str1);
    printf("%s\n", str2);

    // 将str2拼接在str1的后面
    myStrcat(str1, str2);

    // 输出
    printf("%s\n", str1);
    printf("%s\n", str2);

    return 0;
}

输出:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/63402.html

hello
123456
hello123456
123456

string.h中的字符数组拼接

在string.h中,内置了一个strcat方法,用于实现字符数组的拼接。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/63402.html

示例代码:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/63402.html

#include <stdio.h>
#include <string.h>


int main(void) {
    // 定义两个字符数组
    char str1[128] = "hello";
    char str2[128] = "123456";

    // 输出
    printf("%s\n", str1);
    printf("%s\n", str2);

    // 将str2拼接在str1的后面
    strcat(str1, str2);

    // 输出
    printf("%s\n", str1);
    printf("%s\n", str2);

    return 0;
}

输出:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/63402.html

hello
123456
hello123456
123456
文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/63402.html
  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/ymba/63402.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定