C语言程序设计现代方法_第二版,CH13第十三章练习题(Exercises)

CH13_1

Incorrect calls:
(b) printf expects second argument to be of type char to match format specifier %c, second
    argument received is type char *.
(c) printf expects second argument to be of type char * to match format specifier %s, second
    argument received is type char.
(e) printf expects first argument to be type const char *, received type char.
(h) putchar expects type char, received type char *.
(i) puts expects type const char *, received type char.
(j) puts already prints a newline at the end so this statment prints two newline chars.
(k) prints a space and a new line character rather than just a newline char.
//总结:
//对于printf,默认调用char*,除非前方有%d、%c等转换说明指明;
//对于puts,默认调用char*;
//对于putchar,默认调用char;

CH13_2

//按照CH13_1中的分析总结,很容易得到以下结论:
(a) Illegal; p is not a character.
(b) Legal; output is a.
(c) Legal; output is abc.
(d) Illegal; *p is not a pointer.

CH13_3

i = 12
s = abc34
j = 56

CH13_4
(a)

int read_line(char str[], int n)
{
    int ch, i = 0;

    while ((ch = getchar()) != '\n') {
        if (i == 0 && isspace(ch)) {
            ;   //isspace()函数判断是否为空白符,在ctype.h中
        }
        else if (i < n) {
            str[i++] = ch;
        }    
    }
    str[i] = '\0';
    return i;
}

(b)

int read_line(char str[], int n)
{
  int ch, i = 0;

  while (!isspace(ch = getchar()))
    if (i < n)
      str[i++] = ch;
  str[i] = '\0';
  return i;
}

(c)

int read_line(char str[], int n)
{
    int ch, i = 0;

    while ((ch = getchar() != '\n')) {
        if (i < n) {
            str[i++] = ch;
        }   
    }
    str[i] = '\n';
    str[++i] = '\0';
    return i;
}

(d)

int read_line(char str[], int n)
{
  int ch, i;

  for (i = 0; i < n; i++) {
    ch = getchar();
    if (ch == '\n')
      break;
    str[i] = ch;
  }
  str[i] = '\0';
  return i;
}

CH13_5
(a)

void capitalize(char str[])
{
    int i = 0;

    while (str[i] != '\0') {
        str[i] = toupper(str[i]);
        i++;
    }
}

(b)

void capitalize(char str[])
{
    while (*str != '\0') {
        *str = toupper(*str);
        str++;
    }
}

CH13_6

void censor(char str[])
{

    while (*str != '\0') {
        if (*str == 'f' && *(str + 1) == 'o' && *(str + 2) == 'o') {
            *++str = *++str = *str = 'x';
            str += 3;
            continue;
        }
        str++;
    }
}

CH13_7

//(d)与其他语句不等价

CH13_8

tired-or-wired?

CH13_9

"computers\0"

CH13_10

//指针变量q为进行初始化,不知道其指向哪里,无法作为字符串变量;

CH13_11

int strcmp(char* s, char* t)
{
    while (s == t) {
        if (!*s) {
            return 0;
        }
    }
    return *s - *t;
}

CH13_12

void get_extension(const char *file_name, char *extension)
{

    while (*file_name) {
        if (*file_name++ == '.') {
            strcpy(extension, file_name);
            return;
        }
    }
    strcpy(extension, "\n");
}

CH13_13

void build_index_url(const char *domain, char *index_url)
{
    strcpy(index_url, "http://www.");
    strcat(index_url, domain);
    strcat(index_url, "/index.html");
}

CH13_14

Grinch

CH13_15

//(a):3 (b):0
//(c):返回的是不在字符串t中的第一个字符在字符串s中的位置

CH13_16

int count_spaces(const char* s)
{
    int count = 0;

    while (*s) {
        if (*s++ == ' ') {
            count++;
        }
    }  
    return count;
}

CH13_17

bool test_extension(const char* file_name, const char* extension)
{
    while (*file_name++ != '.') {
        ;
    }

    if (strlen(file_name) != strlen(extension)) {
        return false;
    }

    while (*file_name) {
        if (toupper(*file_name++) != toupper(*extension++)) {
            return false;
        }
    }
    return true;
}

CH13_18

void remove_filename(char* url)
{
    char* flag = url;
    while (*url++) {
        if (*url == '/') {
            flag = url;
        }
    }
    *flag = '\0';
}
  • 3
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值