pxxfxxxxx

导航

字串简介

20.字元阵列

  • 字元与字元阵列
  1. 一个字元型别变数可以储放一个字元 char ch = 'H';
  2. 一个字元阵列型别变数可以储放一到多个字元   如:char str [ ] = {'h', 'e', 'l', 'l', 'o','\0'};
  • 字串与字元阵列
  1. 字串是以 '\0' 表示结尾的字元阵列 
  2. 字元阵列可以用 “字串内容” 进行初始化, 会自动加上 '\0' 字元 如:char str[] = ''Hello'';
 1 #include <stdio.h>
 2 
 3 void str_print(char str[])
 4 /*
 5  {
 6     int i;
 7     for (i = 0; str[i] != '\0'; i++) {
 8         printf("%c", str[i]);
 9     }
10     printf("\n");
11 } */
12 
13 {
14     printf("%s\n", str);
15 }
16 int main() {
17     char str[] = "Hello world";
18     str_print(str);
19     return 0;
20 }
21 
22 
23 Hello world
24 
25 Process returned 0 (0x0)   execution time : 11.821 s
26 Press any key to continue.

 20.1 计算字串长度的练习

 1 #include <stdio.h>
 2 
 3 int str_len (char str[]) {
 4     int i = 0;
 5     while (str[i] != '\0') {
 6         i++;
 7     }
 8     return i;
 9 }
10 
11 
12 int main() {
13     char str[11] = "Hello world"; //C 语言允许字元阵列大小刚好只包含有文字内容的部分,但这样的字元阵列不能直接拿来给一般字串处理函式使用
14     printf("Length: %lu\n", sizeof(str));
15     printf("Length: %d\n", str_len(str));// 字串长度
16     return 0;
17 }
18 
19 Length: 11
20 Length: 12
21 
22 Process returned 0 (0x0)   execution time : 1.246 s
23 Press any key to continue.
 1 #include <stdio.h>
 2 
 3 int str_len (char str[]) {
 4     int i = 0;
 5     while (str[i] != '\0') {
 6         i++;
 7     }
 8     return i;
 9 }
10 
11 
12 int main() {
13     char str[12] = "Hello world"; 
14     printf("Length: %lu\n", sizeof(str));
15     printf("Length: %d\n", str_len(str));// 字串长度
16     return 0;
17 }
18 
19 Length: 12
20 Length: 11
21 
22 Process returned 0 (0x0)   execution time : 1.217 s
23 Press any key to continue.

 20.2 从键盘输入读入一行字的练习

 1 #include <stdio.h>
 2 void str_read(char[]);
 3 
 4 int main() {
 5     char str[15];
 6     str_read(str);
 7     printf("%s\n", str);
 8     return 0;
 9 }
10 
11 
12 void str_read(char str[]) {
13         int i = 0;
14         while (1) {
15             scanf("%c", &str[i]);
16             if (str[i] == '\n')
17                 break;
18             i++;
19         }
20         str[i] = '\0';
21 }
22 
23 
24 panfangxu
25 panfangxu
26 
27 Process returned 0 (0x0)   execution time : 9.760 s
28 Press any key to continue.

 22.3 关于读入字串的缓冲区溢位问题

 1 #include <stdio.h>
 2 
 3 void str_read(char[], int);
 4 
 5 int main() {
 6     char str[15];
 7     str_read(str, 7);
 8     printf("%s\n", str);
 9     return 0;
10 }
11 
12 void str_read(char str[], int n) {
13     int i;
14     for (i = 0; i < n; i++) {
15         scanf("%c", &str[i]);
16         if (str[i] == '\n')
17             break;
18     }
19     str[i] = '\0';
20 }
21 
22 qwertyuiopasdfg
23 qwertyu
24 
25 Process returned 0 (0x0)   execution time : 7.086 s
26 Press any key to continue.

 阵列大小在宣告定义后就不能改变

资源有限,不能改变大小就能解决问题

 

20.4使用scanf读入资料的问题

 

 1 #include <stdio.h>
 2 
 3 int main() {
 4     int number;
 5     if (scanf("%d", &number) == 1) {
 6     printf("%d\n", number);
 7 
 8     }else {
 9         printf("Error: Invalid input\n");
10 
11     }
12     return 0;
13 }
14 
15 abc
16 Error: Invalid input
17 
18 Process returned 0 (0x0)   execution time : 2.767 s
19 Press any key to continue.
20 
21 1
22 1
23 
24 Process returned 0 (0x0)   execution time : 3.133 s
25 Press any key to continue.

 

 1 #include <stdio.h>
 2 
 3 int main() {
 4     int number;
 5     while (scanf("%d", &number) != 1) {
 6          printf("Error: Invalid input\n");
 7 
 8     }
 9     printf("%d\n", number);
10     return 0;
11 }
12 
13 abc
14 Error: Invalid input
15 Error: Invalid input
16 ...
17 
18 // scanf 函式读入资料失败的后遗症

 

posted on 2019-05-15 09:23  pxxfxxxxx  阅读(311)  评论(0编辑  收藏  举报