什么是字符串
像 “ABC” 那样带有双引号的一系列字符称为字符串常量。字符串常量末尾会被加上一个null字符的值为0的字符。
字符串常量的长度
代码清单9-1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
#define _CRT_SECURE_NO_DEPRECATE #include <stdio.h> #include <stdlib.h>
int main(void) { printf("sizeof(\"123\") = %u\n", (unsigned)sizeof("123")); printf("sizeof(\"AB\\tC\") = %u\n", (unsigned)sizeof("AB\tC")); printf("sizeof(\"abc\\0def\") = %u\n", (unsigned)sizeof("abc\0def")); return 0; }
|
字符串
字符串适合放在char型数组中存储。字符串的末尾是首次出现的null字符。
代码清单9-2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
#define _CRT_SECURE_NO_DEPRECATE #include <stdio.h> #include <stdlib.h>
int main(void) { char str[4];
str[0] = 'A'; str[1] = 'B'; str[2] = 'C'; str[3] = '\0';
printf("字符串str为\"%s\"。\n", str); return 0; }
|
字符串数组的初始化赋值
为保存字符串而将每个字符逐一赋予数组的各个元素是一件麻烦的事,所以我们可以像下面这样进行声明。
char str[4]={‘A’,‘B’,‘C’,‘\0’};
代码清单9-3
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
#define _CRT_SECURE_NO_DEPRECATE #include <stdio.h> #include <stdlib.h>
int main(void) { char str[] = "ABC";
printf("字符串str为\"%s\"。\n", str); return 0; }
|
空字符串
一个字符也没有的字符串,称为空字符串。
char ns[] = “”;
char ns[] = {‘\0’};
字符串的读取
代码清单9-4
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
#define _CRT_SECURE_NO_DEPRECATE #include <stdio.h> #include <stdlib.h>
int main(void) { char name[48];
printf("请输入您的名字:"); scanf("%s", name);
printf("您好,%s先生/女士!!\n", name); return 0; }
|
格式化显示字符串
代码清单9-5
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
#define _CRT_SECURE_NO_DEPRECATE #include <stdio.h> #include <stdlib.h>
int main(void) { char str[] = "12345";
printf("%s\n", str); printf("%3s\n", str); printf("%.3s\n", str); printf("%8s\n", str); printf("%-8s\n", str); return 0; }
|
字符串数组
字符串可以用数组来表示,所以字符串的集合也可以用数组的数组来表示。
字符串数组
类型相同的数据集合适合用数组来实现,字符串的集合,即字符串数组。
代码清单9-6
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
#define _CRT_SECURE_NO_DEPRECATE #include <stdio.h> #include <stdlib.h>
int main(void) { int i; char cs[][6] = { "Turbo","NA","DOHC" };
for (i = 0; i < 3; i++) { printf("cs[%d] = \"%s\"\n", i, cs[i]); } return 0; }
|
读取字符串数组中的字符串
代码清单9-7
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
#define _CRT_SECURE_NO_DEPRECATE #include <stdio.h> #include <stdlib.h>
int main(void) { int i; char s[3][128]; for (i = 0; i < 3; i++) { printf("s[%d] : ", i); scanf("%s", s[i]); }
for (i = 0; i < 3; i++) printf("s[%d] =\"%s\"\n", i, s[i]); return 0; }
|
字符串处理
字符串长度
代码清单9-8
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
|
#define _CRT_SECURE_NO_DEPRECATE #include <stdio.h> #include <stdlib.h>
int str_length(const char s[]) { int len = 0; while (s[len]) len++;
return len; }
int main(void) { char str[128]; printf("请输入字符串:"); scanf("%s", str);
printf("字符串\"%s\"的长度是%d。\n", str, str_length(str)); return 0; }
|
显示字符串
代码清单9-9
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
|
#define _CRT_SECURE_NO_DEPRECATE #include <stdio.h> #include <stdlib.h>
void put_string(const char s[]) { int i = 0; while (s[i]) putchar(s[i++]); }
int main(void) { char str[128]; printf("请输入字符串:"); scanf("%s", str);
printf("你输入了。"); put_string(str); printf("\n"); return 0; }
|
数字字符的出现次数
代码清单9-10
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
|
#define _CRT_SECURE_NO_DEPRECATE #include <stdio.h> #include <stdlib.h>
void str_dcount(const char s[], int cnt[]) { int i = 0; while (s[i]) { if (s[i] >= '0' && s[i] <= '9') cnt[s[i] - '0']++; i++; } } int main(void) { int i; int dcnt[10] = { 0 }; char str[128];
printf("请输入字符串:"); scanf("%s", str);
str_dcount(str, dcnt);
puts("数字字符的出现次数"); for (i = 0; i < 10; i++) printf("'%d': %d\n", i, dcnt[i]); return 0; }
|
大小写字符转换
代码清单9-11
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
|
#define _CRT_SECURE_NO_DEPRECATE #include <stdio.h> #include <stdlib.h>
void str_toupper(char s[]) { int i = 0; while (s[i]) { s[i] = toupper(s[i]); i++; } }
void str_tolower(char s[]) { int i = 0; while (s[i]) { s[i] = tolower(s[i]); i++; } } int main(void) { char str[128];
printf("请输入字符串:"); scanf("%s", str);
str_toupper(str); printf("大写字母:%s\n", str);
str_tolower(str); printf("小写字母:%s\n", str); return 0; }
|
字符串数组的参数传递
代码清单9-12
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
#define _CRT_SECURE_NO_DEPRECATE #include <stdio.h> #include <stdlib.h>
void put_strary(const char s[][6], int n) { int i; for (i = 0; i < n; i++) printf("s[%d] = \"%s\"\n", i, s[i]); }
int main(void) { char cs[][6] = { "Turbo","NA","DOHC" }; put_strary(cs, 3); return 0; }
|
代码清单9-13
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
|
#define _CRT_SECURE_NO_DEPRECATE #include <stdio.h> #include <stdlib.h>
void put_strary2(const char s[][6], int n) { int i; for (i = 0; i < n; i++) { int j = 0; printf("s[%d] = \"", i);
while (s[i][j]) putchar(s[i][j++]); puts("\""); } }
int main(void) { char cs[][6] = { "Turbo","NA","DOHC" }; put_strary2(cs, 3); return 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 49 50 51 52
|
#define _CRT_SECURE_NO_DEPRECATE #include <stdio.h> #include <stdlib.h> #define STR_LENGTH 128
void put_string_rep(const char s[]) { int i = 0; while (s[i]) putchar(s[i++]); printf(" {"); i = 0; while (s[i]) { putchar('"'); putchar(s[i++]); printf("'"); } printf("'\\0'}\n"); } int main(void) { int i=0; char s[STR_LENGTH]; char ss[5][STR_LENGTH]; printf("字符串s:"); scanf("%s", s);
printf("请输入5个字符串。\n"); for (i = 0; i < 5; i++) { printf("ss[%d]:", i); scanf("%s", ss[i]); } printf("字符串s:"); put_string_rep(s);
printf("字符串数组ss\n"); for (i = 0; i < 5; i++) { printf("ss[%d]:", i); put_string_rep(ss[i]); } return 0; }
|
练习
练习9-1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
#define _CRT_SECURE_NO_DEPRECATE #include <stdio.h> #include <stdlib.h>
int main(void) { char str[] = "ABC\0DEF";
printf("字符串str为\"%s\"。\n", str); return 0; }
|
练习9-2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
#define _CRT_SECURE_NO_DEPRECATE #include <stdio.h> #include <stdlib.h>
int main(void) { char str[] = "\0ABC";
printf("字符串str为\"%s\"。\n", str); return 0; }
|
练习9-3
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
|
#define _CRT_SECURE_NO_DEPRECATE #include <stdio.h> #include <stdlib.h> #define line 5
int main(void) { int i,k; char s[line][128];
for (i = 0; i < line; i++) { printf("s[%d] : ", i); scanf("%s", s[i]);
for (k = 0; s[i][k] == '$'; k++);
if (k == 5) { break; } }
printf("\n");
for (int j = 0; j < i; j++) printf("s[%d] =\"%s\"\n", j, s[j]);
return 0; }
|
练习9-4
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
#define _CRT_SECURE_NO_DEPRECATE #include <stdio.h> #include <stdlib.h>
void null_string(char s[]) { s[0] = '\0'; } int main(void) { int i,k; char s[] = "string";
null_string(s);
printf("字符串s改为空字符串\"%s\"", s);
return 0; }
|
练习9-5
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
|
#define _CRT_SECURE_NO_DEPRECATE #include <stdio.h> #include <stdlib.h>
int str_char(const char s[],int c) { int i,count=0; for (i = 0; s[i] != '\0'; i++) { if (s[i] == c) { count++; break; } }
if (count != 0) { return i; } else { return -1; } } int main(void) { int i,k,key; char str; char s[] = "string";
printf("在%s字符串中输入你要搜索的字符:", s); scanf("%c", &str);
key = (int)str; printf("字符C在字符串s的下标是:%d",str_char(s, key));
return 0; }
|
练习9-6
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
|
#define _CRT_SECURE_NO_DEPRECATE #include <stdio.h> #include <stdlib.h>
int str_char(const char s[],int c) { int i,count=0; for (i = 0; s[i] != '\0'; i++) { if (s[i] == c) { count++; break; } }
if (count != 0) { return count; } else { return 0; } } int main(void) { int i,k,key; char str; char s[] = "string";
printf("在%s字符串中输入你要搜索的字符:", s); scanf("%c", &str);
key = (int)str; printf("在字符串%s中有%d个字符%c",s,str_char(s, key),key);
return 0; }
|
练习9-7
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
#define _CRT_SECURE_NO_DEPRECATE #include <stdio.h> #include <stdlib.h>
void put_stringn(const char s[], int n) { for (int i = 0; i < n; i++) { printf("%s", s); } }
int main(void) { char s[] = "ABC"; put_stringn(s, 3); return 0; }
|
练习9-8
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
#define _CRT_SECURE_NO_DEPRECATE #include <stdio.h> #include <stdlib.h>
void put_stringr(const char s[]) { int i; for (i = 0; s[i] != '\0'; i++);
for(int j=i;j>=0;j--) { printf("%c", s[j]); } }
int main(void) { char s[] = "ABC"; put_stringr(s); return 0; }
|
练习9-9
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
|
#define _CRT_SECURE_NO_DEPRECATE #include <stdio.h> #include <stdlib.h>
void rev_string(char s[]) { char buffer[sizeof(s)] = { 0 }; int j = sizeof(s) - 1;
memcpy(buffer, s, sizeof(s));
for (int i=0; j > 0; i++, j--) { s[i] = buffer[j-1]; } }
int main(void) { char s[] = "ABC"; rev_string(s); printf("%s", s); return 0; }
|
练习9-10
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
|
#define _CRT_SECURE_NO_DEPRECATE #include <stdio.h> #include <stdlib.h>
void del_digit(char s[]) { char buffer[sizeof(s)] = {0};
for (int i=0,j=0;s[i] != '\0'; i++) { if (s[i] > '9') { buffer[j] = s[i]; j++; } }
memcpy(s, buffer, sizeof(buffer)); }
int main(void) { char s[] = "AB1C3"; del_digit(s); printf("%s", s); return 0; }
|
练习9-11
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
|
#define _CRT_SECURE_NO_DEPRECATE #include <stdio.h> #include <stdlib.h> #define line 5 #define row 128
void put_strary(const char s[line][row], int n) { int i, k; for (i = 0; i < n; i++) { printf("s[%d] : ", i); scanf("%s", s[i]);
for (k = 0; s[i][k] == '$'; k++);
if (k == 5) { break; }
}
printf("\n");
for (int j = 0; j < i; j++) printf("s[%d] =\"%s\"\n", j, s[j]); }
int main(void) { char cs[line][row] = { 0 }; put_strary(cs, 3);
return 0; }
|
练习9-12
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
|
#define _CRT_SECURE_NO_DEPRECATE #include <stdio.h> #include <stdlib.h>
void rev_string(char s[][128],int n) { char buffer[sizeof(s)][128] = {0}; int j = 0;
memcpy(buffer, s, sizeof(s));
for (int i = 0; i <= sizeof(s)/128; i++) { for (int k = n,j=0; j < n; j++,k--) { s[i][j] = buffer[i][k-1]; } } }
int main(void) { char str[][128] = { "ABCD" }; rev_string(str, 3); printf("%s", str); return 0; }
|