第十章 字符串

  • 字符串(character String);

    • 一个或多个字符的序列称为字符串
    • c语言中形如“My heart siill.”
    • 双引号不是字符串的一部分,仅用来告知编译器括起来的是字符串
  • C语言中的字符串

    • 使用字符数组存储

见图l

l

  • 注意:

    空字符不要和NULL混淆

    空字符是字符串的终止符,而NULL是一个符号,表示不引用任何内容的内存地址

字符串与字符数组

  • 使用字符数组存放字符串
1
2
3
4
5
6
7
8
#include <stdio.h>
void main()
{
char name1[8] = {'J','a','c','k','s','o','n','\0'};
char name2[] = "Jackson";
printf("%s\n",name1);
printf("%s\n",name2);
}
1
2
3
4
5
6
7
8
9
#include <stdio.h>
void main()
{
char words[50];
printf("请输入歌词:");
//words是数组,不需要&符号
scanf("%s\n",words);
printf("%s\n",words);
}
  • 注意:声明存储字符串的数组时,数组大小至少比所存储的字符多1,因为编译器会自动在字符串常量的末尾添加空字符\0

gets/puts函数补充

1
2
3
4
5
6
7
8
9
10
11
12
#include <stdio.h>
void main()
{
char words1[50];
char words2[50];
printf("请输入歌词:");
gets(words1);
gets(words2);
puts("*************\n");
puts(words1);
puts(words2);
}
  • 注意:

    1、gets函数不对接受字符串的buffer进行边界检测,会造成越界,从而产生bug

    2、可以使用fgets(words1,20,stdin);代替gets,20表示最多读入20-1个字符

字符串操作

  • 常用字符串处理函数

见图m

m

strlen函数

  • 功能:
    • 计算字符串的实际长度,不含字符串结束标志\0
1
2
3
4
5
6
7
8
9
10
#include <stdio.h> //#include <string.h>
void main()
{
char words1[] = {'H','i','\0','K','\0'};
char words2[] = "Jackson";
char words3[] = "你好,世界!";
printf("words1长度为:%d\n",strlen(words1));
printf("words2长度为:%d\n",strlen(words2));
printf("words3长度为:%d\n",strlen(words3));
}

字符串复制

  • 功能:
    • 把源字符数组中的字符串复制到目的字符数组中,连同结束标志\0一同复制
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void main()
{
char str1[50];
char str2[20];
printf("输入目的字符串:");
gets(str1);
printf("输入源字符串:");
gets(str2);
strcpy(str1,str2);

printf("****复制后****\n");
printf("目的字符串:%s\n",str1);
printf("源字符串:%s\n",str2);
}

字符串比较

  • 功能:
    • 将两个字符串从首字母开始,按照ASCII码的顺序进行逐个比较
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define USER_NAME "admin"
#define PASSORD "admin"
char* MyFgets(char[], int);//fgets封装函数,去\n

/**
* 验证传入的用户名和密码是否正确
* 参数1:需要验证的用户名
* 参数2:需要验证的密码
* 返回:如果用户名和密码合法,返回1,否则返回0
*/
int login(char[], char[]);
int main()
{
//演示字符串的连接
char str1[100] = "登";
char str2[20] = "录";
strcat_s(str1,20, str2);
printf("%s\n", str1);//会把str2接入str1中str2不变

char userName[50];
char password[50];

printf("请输入用户名:");
MyFgets(userName, 50);
printf("请输入密码:");
MyFgets(password, 50);
if (login(userName, password) == 1)
{
printf("恭喜,登录成功!\n");
}
else
{
printf("输入错误!\n");
}

return 0;
}
int login(char userName[], char password[])
{
int result = 0;

if (strcmp(USER_NAME, userName) == 0 && strcmp(PASSORD, password) == 0)
{
//验证合法
result = 1;
}

return result;
}
char* MyFgets(char* str, int maxLen)
{
fgets(str, maxLen, stdin);
//去掉最后一位空格
//找到最后一位
int count = 0;
while (str[count] != '\n')
{
if (str[count] == '\n')
break;
count++;
}
//循环后count就是\n下标
str[count] = '\0';
}

随堂练习

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <stdio.h>
#include <stdlib.h>
//实现字符串的加密与解密
//加密方式:将字符串中每一个字符加上它在字符串中的位置和一个偏移量5
//例如:xuetang9中,第一个字符x在字符串中的位置为0,那么对应的密文是'm'+0+5

#define KEY 5//偏移量/密钥

/**
* 加密传入的字符串
* 参数1:要加密的字符串
* 返回值:返回加密后的字符串
*/
char* encrypt(char[]);
char* dencrypt(char[]);
int main()
{
char password[50] = "123456";
encrypt(password);
printf("加密后的字符串为:%s\n", password);
dencrypt(password);
printf("解密后的字符串为:%s\n", password);
return 0;
}
char* encrypt(char password[])
{
int i = 0;
int count = strlen(password);//字符串的长度
for (i = 0; i < count; i++)
{
//将字符串中每个字符加上它在字符串中的位置和一个偏移量5
password[i] = password[i] + i + KEY;
}
return password;
//字符串最后的\0是否需要替换?-不需要
}
char* dencrypt(char password[])
{
int i = 0;
int count = strlen(password);//字符串的长度
for (i = 0; i < count; i++)
{
//将字符串中每个字符加上它在字符串中的位置和一个偏移量5
password[i] = password[i] - i - KEY;
}
return password;
//字符串最后的\0是否需要替换?-不需要
}

指向字符串的指针

  • 将指针指向字符串
    • 可以指向常量字符串
    • 也可以指向存储字符串的字符数组
1
2
3
4
char * words = "My heart is still.";
words += 9;
puts(words);
//输出is still.

见图n

n

数组和指针

  • 数组形式和执行形式的不同
    • 初始化字符数组时会把静态存储区的字符串拷贝到数组中
    • 初始化指针时只把字符串的地址拷贝给指针
1
2
3
4
5
6
7
8
#include <stdio.h>
void main()
{
char str[] = "For the Horde";
printf("字符串常量的地址:%p\n","For the Horde");
printf("字符数组的首地址:%p\n",str);
printf("字符指针的取值 %p\n",ptr_str);
}
更新于

请我喝[茶]~( ̄▽ ̄)~*

Chen 微信支付

微信支付

Chen 支付宝

支付宝

Chen 贝宝

贝宝