12-第11章复习题答案
A.11 第11章复习题答案
1.如果希望得到一个字符串,初始化列表中应包含 '\0' 。当然,也可以用另一种语法自动添加空字符:
char name[] = "Fess";
2.
See you at the snack bar.
ee you at the snack bar.
See you
e you
3.
y
my
mmy
ummy
Yummy
4. I read part of it all the way through.
5.a. Ho Ho Ho!!oH oH oH
b.指向 char 的指针(即, char *)。
c.第 1 个 H 的地址。
d. --pc 的意思是把指针递减 1 ,并使用存储在该位置上的值。 -- pc 的意思是解引用 pc 指向的值,然后把该值减 1 (例如, H 变成 G )。
e. Ho Ho Ho!!oH oH o
注意 在两个!之间有一个空字符,但是通常该字符不会产生任何打印的效果。
f. while ( * pc) 检查 pc 是否指向一个空字符(即,是否指向字符串的末尾)。 while 的测试条件中使用存储在指针指向位置上的值。
while (pc - str) 检查 pc 是否与 str 指向相同的位置(即,字符串的开头)。 while 的测试条件中使用存储在指针指向位置上的值。
g.进入第 1 个 while 循环后, pc 指向空字符。进入第 2 个 while 循环后,它指向空字符前面的存储区(即, str 所指向位置前面的位置)。把该字节解释成一个字符,并打印这个字符。然后指针退回到前面的字节处。永远都不会满足结束条件 (pc == str) ,所以这个过程会一直持续下去。
h.必须在主调程序中声明 pr() : char pr(char );
6.字符变量占用一个字节,所以 sign 占 1 字节。但是字符常量存储为 int 类型,意思是 '$' 通常占用 2 或 4 字节。但是实际上只使用 int 的 1 字节存储 '$' 的编码。字符串 "$" 使用 2 字节:一个字节存储 '$' 的编码,一个字节存储的 '\0' 编码。
7.打印的内容如下:
How are ya, sweetie? How are ya, sweetie?
Beat the clock.
eat the clock.
Beat the clock. Win a toy.
Beat
chat
hat
at
t
t
at
How are ya, sweetie?
8.打印的内容如下:
faavrhee
*le*on*sm
9.下面是一种方案:
#include <stdio.h> // 提供fgets()和getchar()的原型
char * s_gets(char * st, int n)
{
char * ret_val;
ret_val = fgets(st, n, stdin);
if (ret_val)
{
while (*st != '\n' && *st != '\0')
st++;
if (*st == '\n')
*st = '\0';
else
while (getchar() != '\n')
continue;
}
return ret_val;
}
10.下面是一种方案:
int strlen(const char * s)
{
int ct = 0;
while (*s++) // 或者while (*s++ != '\0')
ct++;
return(ct);
}
11.下面是一种方案:
#include <stdio.h> // 提供 fgets()和getchar()的原型
#include <string.h> // 提供 strchr()的原型
char * s_gets(char * st, int n)
{
char * ret_val;
char * find;
ret_val = fgets(st, n, stdin);
if (ret_val)
{
find = strchr(st, '\n'); // 查找换行符
if (find) // 如果地址不是 NULL,
*find = '\0'; // 在此处放置一个空字符
else
while (getchar() != '\n')
continue;
}
return ret_val;
}
12.下面是一种方案:
#include <stdio.h> /* 提供 NULL 的定义 */
char * strblk(char * string)
{
while (*string != ' ' && *string != '\0')
string++; /* 在第1个空白或空字符处停止 */
if (*string == '\0')
return NULL; /* NULL 指空指针 */
else
return string;
}
下面是第 2 种方案,可以防止函数修改字符串,但是允许使用返回值改变字符串。表达式 (char * )string 被称为“通过强制类型转换取消 const ”。
#include <stdio.h> /*提供 NULL 的定义*/
char * strblk(const char * string)
{
while (*string != ' ' && *string != '\0')
string++; /*在第1个空白或空字符处停止*/
if (*string == '\0')
return NULL; /* NULL 指空指针*/
else
return (char *)string;
}
13.下面是一种方案:
/* compare.c -- 可行方案 */
#include <stdio.h>
#include <string.h> // 提供strcmp()的原型
#include <ctype.h>
#define ANSWER "GRANT"
#define SIZE 40
char * s_gets(char * st, int n);
void ToUpper(char * str);
int main(void)
{
char try[SIZE];
puts("Who is buried in Grant's tomb?");
s_gets(try, SIZE);
ToUpper(try);
while (strcmp(try, ANSWER) != 0)
{
puts("No, that's wrong. Try again.");
s_gets(try, SIZE);
ToUpper(try);
}
puts("That's right!");
return 0;
}
void ToUpper(char * str)
{
while (*str != '\0')
{
*str = toupper(*str);
str++;
}
}
char * s_gets(char * st, int n)
{
char * ret_val;
int i = 0;
ret_val = fgets(st, n, stdin);
if (ret_val)
{
while (st[i] != '\n' && st[i] != '\0')
i++;
if (st[i] == '\n')
st[i] = '\0';
else
while (getchar() != '\n')
continue;
}
return ret_val;
}