练习9-1
/*---输出字符数组char str[] = "ABC\0DEF"---*/ #include <stdio.h>int main(void){char str[] = "ABC\0DEF";printf("字符串str为\"%s\"。\n", str);return 0;}
练习9-2
/*---让该初始化赋值得到的字符串s变成空字符串:char s[] = "ABC"---*/#include <stdio.h>int main (void){char s[] = "ABC";s[0] = '\0';printf("字符串s为:%s",s);return 0;}
练习9-3
/*对代码清单9-7进行改写*/#include <stdio.h>#define NUMBER 5int main(void){int i;char s[NUMBER][128];for (i = 0; i < NUMBER; i++) {printf("s[%d]:", i);scanf("%s", s[i]);if (strcmp(s[i], "$$$$$") == 0) break;}for(i = 0; i < NUMBER; i++){if (strcmp(s[i], "$$$$$") == 0)break;printf("s[%d] = \"%s\"\n", i, s[i]);}return 0;}
练习9-4
/*---编写一个函数,使字符串s为空字符串。---*/#include <stdio.h>void null_string(char s[]){s[0] = '\0';printf("字符串s为:%s",s);}int main(void){char s[] = "HELLOWORLD";null_string(s);return 0;}
练习9-5
/*---编写函数,若字符串s中含有字符c(若含有多个,以先出现的为准),则返回该元素的下标。---*/#include <stdio.h>int str_char(const char s[], int c) {int idx = 0;while (s[idx]){if(s[idx] == 'c')return idx;idx++;}return -1;}int main(void){char c;char s[] = "hellochina";printf("字符c在字符串%s中的下标为:%d", s, str_char(s,c));return 0;}
练习9-6
/*---编写函数,返回字符串s中字符c的个数---*/#include <stdio.h>int str_chnum(const char s[], int c){int num = 0, idx = 0;while (s[idx]){if(s[idx] == 'c')num++;idx++;}return num;if(num == 0)return 0;}int main(void){char c = 'c';char s[] = "hellochinanancang";printf("字符串%s中%c字符的个数为%d", s, c, str_chnum(s, c));return 0;}
练习9-7
/*---编写函数,使字符串s显示n次。---*/#include <stdio.h>void put_stringn(const char s[], int n){int num, i = 0;for (num = 0; num < n; num++){//printf("%s\n",s);while(s[i]){putchar(s[i++]);}i = 0;}return 0;}int main(void){char s[] = "多重影分身之术!";int n = 100;put_stringn(s, n);return 0;}
练习9-8
/*---编写函数,实现字符串的逆向输出。---*/#include <stdio.h>void put_stringr(const char s[]){int num = 0, i = 0;while (s [num])num++;while (i < num){putchar(s[num - i -1]);i++;}}int main(void){char s [] = "hello";put_stringr(s);return 0;}
练习9-9
/*---逆向显示字符串s的字符---*/#include <stdio.h>void rev_string(char s[]){int num = 0, i = 0, temp;while (s [num])num++;for(i = 0; i < num/2; i++){temp = s[i];s[i] = s[num - i -1];s[num - i -1] = temp;}printf("%s",s);}int main(void){char s[] = "hello";rev_string(s);return 0;}
练习9-10
/*---将字符串s中的数字字符全部删除。---*/#include <stdio.h>void del_digit(char s[]) {int num, i = 0, temp ;while (s[i]) {if (s[i] >= '0' && s[i] <= '9'){temp = i;while(s[temp]) {//将0~9数字通过遍历使其被最后一位元素‘\0’替换。s[temp] = s[temp + 1];temp++;}i--;}i++;}printf("%s",s);}int main (void){char s[] = "adb34fd43";del_digit(s);return 0;}
练习9-11
/*---对代码清单进行改写---*/#include <stdio.h>#define NUMBER 5#define IDX_NUM 128void put_strary(const char s[][IDX_NUM], int n){int i;for (i = 0; i < n; i++){if (strcmp(s[i], "$$$$$") == 0) break;printf("s[%d] = \"%s\"\n", i, s[i]);}} int main(void){int i;char cs[NUMBER][IDX_NUM];puts("请输入五个字符串。");for(i = 0; i < NUMBER; i++){scanf("%s",cs[i]);if (strcmp(cs[i], "$$$$$") == 0) break;} put_strary(cs, NUMBER);return 0;}
练习9-12
/*---将所接收的字符串数组中存储的n个字符串的字符逆向显示。---*/#include <stdio.h>void rev_string(char s[][128], int n) {int i=0, j=0, a = 0, temp;for (i = 0; i < n; i++){//遍历字符串数组 while(s[i][j]) {j++; //计算不同字符串的长度 }for(a = 0; a < j/2; a++){//对字符串进行逆向转换 temp = s[i][a];s[i][a] = s[i][j - a - 1];s[i][j - a -1] = temp;}}}int main (void){int n = 3, i;char s[][128] = {"sec", "abc","cartman"};rev_string(s,n);for(i = 0;i < n; i++)printf("%s\n",s[i]);return 0;}
字符串字面量:像"ABC"那样带双引号的一系列字符称为字符串字面量(string literal)。
由3个字符组成的字符串字面量“ABC”实际上占用了4个字符的内存空间。在字符串字面量的末尾会被加上一个
叫作null字符的值为0的字符。用八进制转义字符表示null字符就是‘\0’。整数常量就是0。
字符串:字符串最适合放在char数组中存储。
字符串的读取:为了从标准输入读取字符串,需要把scanf函数的转换说明设为%s,还必须传入数组名。请注意这里的name是不带&运算符的。另外,scanf函数在将从键盘读取的字符串存储到数组中时,会在末尾加上null字符。
格式化显示字符串:
strcmp函数:对两个字符串进行相等比较,若两个字符串相等,则返回0。