C语言基础05--字符串操作

Catalogue
  1. 1. C语言基础05–字符串操作
    1. 1.0.1. 字符串的声明
    2. 1.0.2. 字符串的输入
    3. 1.0.3. 字符串的输出
    4. 1.0.4. 计算字符串长度
    5. 1.0.5. 字符串拼接
    6. 1.0.6. 字符串对比
    7. 1.0.7. 字符串拷贝
    8. 1.0.8. 字符串格式化
    9. 1.0.9. 动态内存管理

C语言基础05–字符串操作

字符串的声明

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include<stdio.h>

int main() {
//字符串的声明可以用3钟方法
//在c/c++里,字符串以00结尾,第一种方式声明会自动在字符串末尾加00
char str[] = { "hello" };
//第二种方式声明需要手动添加\0
char str1[] = { 'h','e','l','l','o','\0' };
//第三种方式声明和声明数组是一样的
char * str2 = "hello";

printf("%s,%s,%s", str, str1, str2);
return 0;
}

字符串的输入

3种方法从控制台接收字符串

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include<stdio.h>

int main() {
//三种从控制台获取输入的途径:gets/fgets/scanf
char str[50];
//gets 默认以回车的方式结束输入
//gets(str);

//fgets接收3个参数,存放字符串的位置,接收字符串的长度,接收模式 与gets的区别在于会接收回车,能控制接收大小
//fgets(str,50,stdin);

//scanf通过格式化输入来控制输入长度
scanf("%5s", &str);
printf("%s", str);
return 0;
}

字符串的输出

1
2
3
4
5
6
7
8
9
#include<stdio.h>

int main() {
char str[] = {"hello\n"};
//两种方式:puts printf
puts(str);//自带换行
printf("%d", str);
return 0;
}

计算字符串长度

1
2
3
4
5
6
7
8
9
10
#include<stdio.h>
#include<string.h>
//字符串操作一般都包含进去,会用得上
int main() {
//字符串以00或者\0结尾,真实长度比输出来的长度要多1
char str[] = {"hello"};
int a = strlen(str);
printf("a=%d", a);
return 0;
}

字符串拼接

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include<stdio.h>
#include<string.h>

int main() {
//字符串拼接的两个函数:strcat,strncat
//strcat接收两个参数:目的字符串,源字符串,将源字符串拷贝到目的字符串后面,存到目的字符串里
//strncat接收三个参数:目的字符串,源字符串,接收长度,将指定长度的源字符串拷贝到目的字符串后面,存到目的字符串里
char str1[] = "hello ";
char str2[] = "world \n";
//strcat(str1, str2);
//printf("%s", str1);
strncat(str1, str2,2);
printf("%s", str1);
return 0;
}

字符串对比

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include<stdio.h>
#include<string.h>

int main() {
//strcmp strcnmp
//strcmp接收两个参数:待比较的两个字符串,返回0表示相等,返回其他值表示不相等
//strncmp接收三个参数,待比较的两个字符串和比对的字符数(从头往后数),返回值同上
char str1[] = "hello";
char str2[] = "hello world";
int res = 0;
//res = strcmp(str1, str2); //-1
res = strncmp(str1, str2, 5); //0
printf("%d", res);

return 0;
}

字符串拷贝

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include<stdio.h>
#include<string.h>

int main() {
//strcpy strncpy
//strcpy接收两个参数:目的字符串和源字符串,将源字符串的东西拷贝到目的字符串里
//strncpy比上面多接收一个参数,拷贝的字符数
char str1[30] = { 0 };//这里需要用0初始化,因为字符串找到0才结尾,打印的时候会出现烫烫烫烫烫烫
char str2[] = "hello world";
//strcpy(str1, str2);
strncpy(str1, str2,5);
printf("%s", str1);


return 0;
}

字符串格式化

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include<stdio.h>
#include<string.h>

int main() {
//sprintf:将printf格式化输出的结果保存到字符串里
//接收三个参数,一个是目标字符串,2是格式化格式,3是格式化内容
char str[] = {"hello"};
int n = 10;

printf("%s,%d\n", str, n);
char str0[20] = { 0 };
sprintf(str0, "%s,%d\n", str, n);
printf("%s",str0);

return 0;
}
//两个输出是一样的

动态内存管理

三步骤:

  1. 申请内存 malloc,memset
  2. 使用内存
  3. 释放内存 free
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include<stdio.h>
#include<string.h>

int main() {
char * str;
//malloc在堆上面申请内存,返回的是void *
str = (char *)malloc(200 * sizeof(char));
//memset,把一段内存地址刷成你想要的的值
memset(str, 0, 200 * sizeof(char));
strcpy(str, "asdjaljfalkjas");
//free 释放内存
free(str);

return 0;
}