练习9-1
将代码清单9-3中数组str的声明改为下面这样,查看程序的运行结果。
charstru =“ABC\ ODEF”
#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";
printf("原先字符串s为\"%s\"。\n", s);
s[0] = '\0';
printf("现在字符串s为\"%s\"。\n", s);
return 0;
}
练习9-3
编写一段程序,对代码清单9-7进行如下改写。
将字符串的个数3改为更大的数,将其值定义为对象式宏。在最初的for语句读取" " 时停止读取操作。第二个 f o r 语句显示 " "时停止读取操作。 第二个 for语句显示" "时停止读取操作。第二个for语句显示" "前输入的所有字符串。
#include <stdio.h>
#include <string.h>
#define NUM 4
int main(void) {
char s[NUM][128];
int i, j;
for (i = 0; i < NUM; ++i) {
printf("s[%d]:", i);
scanf("%s", s[i]);
/* 如果输入的是"$$$$$",提前结束输入 */
if (strcmp("$$$$$", s[i]) == 0)
break;
}
for (j = 0; j < i; ++j)
printf("s[%d] = \"%s\"\n", j, s[j]);
return 0;
}
综习9-4
编写一个函数,使字符串s为空字符串。
void nullstring (char s[]) {/…/]
#include <stdio.h>
void null_string(char s[]) {
s[0] = '\0';
}
int main(void) {
char s[] = "ABC";
printf("原先字符串s为\"%s\"。\n", s);
null_string(s);
printf("现在字符串s为\"%s\"。\n", s);
return 0;
}
练习9-5
编写如下函数,若字符串s中含有字符c(若含有多个,以先出现的为准),则返回该元素的下标值,否则返回-1。
int str char(const char s[], int c){/…/)
#include <stdio.h>
int str_char(const char s[], int c) {
int i;
for (i = 0; s[i]; ++i)
if (s[i] == c)
return i;
return -1;
}
int main(void) {
printf("%d\n", str_char("hello", 'l'));
return 0;
}
练习9-6
编写如下函数,返回字符串s中字符c的个数(没有则返回0)。
int str chnum(const char s[],int c){/…/ }
#include <stdio.h>
int str_chnum(const char s[], int c) {
int num = 0;
int i;
for (i = 0; s[i]; ++i)
if (s[i] == c)
++num;
return num;
}
int main(void) {
printf("%d\n", str_chnum("hello", 'l'));
return 0;
}
练习9-7
编写如下函数,使字符串s显示n次。
void put stringn (const char s[],int n){/…/}
例如,若s和n分别为"ABC"和3,则显示“ABCABCABC"。
#include <stdio.h>
void put_stringn(const char s[], int n) {
int i;
while (--n >= 0)
for (i = 0; s[i]; ++i)
putchar(s[i]);
}
int main(void) {
put_stringn("你好", 3);
putchar('\n');
return 0;
}
练习9-8
编写如下函数,实现字符串的逆向输出。
void put stringr(const char s[]){/…/}
例如,将"SEC"显示为“CEs"。
#include <stdio.h>
void put_stringr(const char s[]) {
int i = 0;
while (s[i]) // 让i处于处于空字符处
++i;
while (--i != -1)
putchar(s[i]);
}
int main(void) {
put_stringr("hello");
putchar('\n');
return 0;
}
练习9-10
编写如下函数,将字符串s中的数字字符全部删除。
void del digit (char s[]) {/…/}
例如传入“AB1C9"则返回“ABC"。
#include <stdio.h>
void del_digit(char s[]) {
int i, j;
for (i = 0, j = 0; s[i]; ++i)
if (s[i] < '0' || s[i] > '9')
s[j++] = s[i];
s[j] = '\0';
}
int main(void) {
char s[] = "AB1C9";
puts(s);
del_digit(s);
puts(s);
return 0;
}
练习9-11
编写一段程序,对代码清单9-12进行如下改写。
将字符串的个数3改为更大的数,将其值定义为对象式宏。
将字符串的字符数6改为128,将其值也定义为对象式宏。
生成读取字符串数组的函数。和练习9-3一样,
在读取"S S S S ”时停止读取操作。显示 " S S ”时停止读取操作。显示"SS ”时停止读取操作。显示"SS$S”前输入的所有字符串。
#include <stdio.h>
#include <string.h>
#define NUM 4
#define MAX 127
void put_strary(const char s[][MAX + 1], int n) {
int i;
for (i = 0; i < n; ++i)
printf("s[%d] = \"%s\"\n", i, s[i]);
}
int read_strary(char s[][MAX + 1], const int n) {
int i;
for (i = 0; i < n; ++i) {
printf("s[%d]:", i);
scanf("%s", s[i]);
if (strcmp("$$$$$", s[i]) == 0)
break;
}
return i;
}
int main(void) {
char s[NUM][MAX + 1];
int n = read_strary(s, MAX);
put_strary(s, n);
return 0;
}
练习9-12
编写如下函数,将所接收的字符串数组中存储的n个字符串的字符逆向显示。
void rev string(char s[] [128],int n) {/…/】
例如,若s中接收的是 {“SEC”,“ABC" ),则将其更新为{ “CES”,“CBA” )。
#include <stdio.h>
#include <string.h>
void rev_strings(char s[][128], int n) {
int i, j;
char t;
while (--n >= 0)
for (i = 0, j = strlen(s[n]) - 1; i < j; ++i, --j) {
t = s[n][i];
s[n][i] = s[n][j];
s[n][j] = t;
}
}
int main(void) {
char s[][128] = {"123", "345"};
rev_strings(s, 2);
puts(s[0]);
puts(s[1]);
return 0;
}