自学C第19天

C Primer Plus

第11章

复习题5:

#include<stdio.h>

char* pr(char* str); #注:pr表示一个指向str的指针,str也是一个指针,所以pr是指针的指针

int main(void)

{

       char* x;

       x = pr("Ho Ho Ho!");

      

       return 0;

}

char* pr(char* str) {

       char* pc;

       pc = str;

       while (*pc)

              putchar(*pc++);#注:*解引用的优先级和++一样,所以重右往左带入运算符,++先执行,之后是*

       do {

              putchar(*--pc);

       } while (pc - str);

       return pc;

复习题9:

#include<stdio.h>

#include<string.h>

#define ANSWER "Grant"

#define SIZE 40

char* s_gets(char* st, int n);

int main(void)

{

       char try[SIZE];

       puts("Who is buried in Grant's tomb?");

       s_gets(try, SIZE);

       while (strcmp(try, ANSWER) != 0) {

              puts("No, that's wrong. Try again.");

              s_gets(try, SIZE);

       }

       puts("That's right!");

       return 0;

}

char* s_gets(char* st, int n) { #注:修改的函数在这里,书中的函数声明是带两个*号的,但尝试去掉s_gets前面的*号 发现先程序还是可以正常运行,不是很清楚这个*号的意义。但该函数在最后返回了一个 ret_val,该变量是一个指针,意思是当函数需要返回指针时,函数声明时需要在函数名前面加上*号,进行强调。

       char* ret_val;

       int i = 0;

       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:

#include <stdio.h>

int strlen(char* pr);

int main(void)

{

       int x;

       char* source = "a lsjf";

       x = strlen(source);

       printf("%d", x);

       return 0;

}

int strlen(char* pr) { #注:计算的字符长度包含空格。

       int count = 0;

       while (*pr++) {

              if (*pr == '\n')#注:这里的if不需要,应为当列遍字符串时,字符串末尾有一个空字符为0,while为0时会跳出循环。

                     break;

              else

                     count++;

       }

       return count;

}

复习题11:

#include<stdio.h>

#include<string.h>

#define ANSWER "Grant"

#define SIZE 40

char* s_gets(char* st, int n);

int main(void)

{

       char try[SIZE];

       puts("Who is buried in Grant's tomb?");

       s_gets(try, SIZE);

       while (strcmp(try, ANSWER) != 0) {

              puts("No, that's wrong. Try again.");

              s_gets(try, SIZE);

       }

       puts("That's right!");

       return 0;

}

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');#注:书中描述的strchr有点让人误解,书中解释为:如果s字符串中包含c(也就是带入的实参'\n')该函数返回指向s字符串首位置的指针,这样会让人理解为返回的指针是st[0]的位置,但让人理解的描述应该是在字符串 s 中查找字符 c,返回字符 c 第一次在字符串 s 中出现的位置,如果未找到字符 c,则返回 NULL,查找的顺序是重左向右,而strrchr是重右向左查找。

              if (find)

                     *find = '\0';

              else

                     while (getchar() != '\n')

                            continue;

       }

       return ret_val;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值