当前位置:   article > 正文

《明解C语言》第三版 (入门篇) 第十一章练习答案

《明解C语言》第三版 (入门篇) 第十一章练习答案

练习11-1

  1. #include <stdio.h>
  2. int main(void)
  3. {
  4. char* p = "123";
  5. printf("p = \"%s\"\n", p);
  6. p = "456"+1;
  7. printf("p = \"%s\"\n", p);
  8. return 0;
  9. }
  10. //输出“56”,p指向的地址+1后,往后移了一位,读到的内容从“456”变成了“56\0".

练习11-2

  1. #include <stdio.h>
  2. #define str_add(arr) ((sizeof(arr)) / (sizeof(arr[0])))
  3. int main(void)
  4. {
  5. char s[][5] = { "LISP","C","Ada" };
  6. char* p[] = { "PAUL","X","MAC","PC","Linux" };
  7. int i;
  8. int x = str_add(s);
  9. int y = str_add(p);
  10. for (i = 0; i < x; i++) {
  11. printf("x = \"%s\"\n",s[i]);
  12. }
  13. for (i = 0; i < y; i++) {
  14. printf("x = \"%s\"\n",p[i]);
  15. }
  16. return 0;
  17. }

练习11-3

  1. #include <stdio.h>
  2. /*--- 将字符串s复制到d ---*/
  3. char* str_copy(char* d, const char* s)
  4. {
  5. char* t = d;
  6. while (*d++ = *s++)
  7. ;
  8. return t;
  9. }
  10. int main(void)
  11. {
  12. char str[128] = "ABC";
  13. char tmp[128];
  14. printf("str = \"%s\"\n", str);
  15. printf("复制的是:", tmp);
  16. scanf("%s", tmp);
  17. puts("复制了。");
  18. printf("str = \"%s\"\n", str_copy(str, tmp));
  19. return 0;
  20. }

练习11-4

  1. #include <stdio.h>
  2. void put_string(const char* s)
  3. {
  4. putchar(*s);
  5. while (*s++)
  6. {
  7. putchar(*s);
  8. }
  9. }
  10. int main()
  11. {
  12. char s[123] ;
  13. printf("请输入字符串:");
  14. scanf("%s",s);
  15. put_string(s);
  16. return 0;
  17. }

练习11-5

  1. #include <stdio.h>
  2. int str_chnum(const char* s, int c)
  3. {
  4. int cnt = 0;
  5. while (*s) // while (*s != NULL)
  6. {
  7. if (*s == c)
  8. {
  9. cnt += 1;
  10. }
  11. *s++;
  12. }
  13. return cnt;
  14. }
  15. int main(void)
  16. {
  17. char str[128];
  18. char cx;
  19. printf("请输入要查找的字符:");
  20. scanf("%c", &cx);
  21. printf("请输入一个字符串:");
  22. scanf("%s", str);
  23. printf("字符串\"%s\"中字符要找的个数为%d\n", str, str_chnum(str, cx));
  24. return 0;
  25. }

练习11-6

意思是:输入要找的 a 字符,在字符串 bash 中,输出结果是ash;在bdsadasd中含有多个a

以最先出现的为准,结果是adasd

  1. #include <stdio.h>
  2. char* str_chnum(const char* s, int c)
  3. {
  4. while (*s++)
  5. {
  6. char* t = s;
  7. if (*s == c) {
  8. return t;
  9. break;
  10. }
  11. }
  12. return NULL;
  13. }
  14. int main() {
  15. char s[128];
  16. char c;
  17. printf("要计数的字符是:");
  18. scanf("%c", &c);
  19. printf("请输入字符串:");
  20. scanf("%s", s);
  21. printf("%s", str_chnum(s, c));
  22. return 0;
  23. }

练习11-7

  1. #include <ctype.h>
  2. #include <stdio.h>
  3. /*--- 将字符串中的英文字符转为大写字母 ---*/
  4. void str_toupper(char* s)
  5. {
  6. while (*s)
  7. {
  8. *s = toupper(*s);
  9. *s++;
  10. }
  11. }
  12. /*--- 将字符串中的英文字符转为小写字母 ---*/
  13. void str_tolower(char* s)
  14. {
  15. while (*s)
  16. {
  17. *s = tolower(*s);
  18. *s++;
  19. }
  20. }
  21. int main()
  22. {
  23. char str[128];
  24. printf("请输入字符串:");
  25. scanf("%s", str);
  26. str_toupper(str);
  27. printf("大写字母:%s\n", str);
  28. str_tolower(str);
  29. printf("小写字母:%s\n", str);
  30. return 0;
  31. }

练习11-8

  1. #include <stdio.h>
  2. void del_digit(char* ptr)
  3. {
  4. char temp[128];
  5. char* p = temp;
  6. while (*ptr != NULL)
  7. {
  8. if (*ptr < '0' || *ptr>'9')
  9. {
  10. *p = *ptr;
  11. p++;
  12. }
  13. ptr++;
  14. }
  15. *p = '\0';
  16. printf("%s", temp);
  17. }
  18. int main()
  19. {
  20. char str[128];
  21. printf("输入字符串str = ");
  22. scanf("%s", str);
  23. printf("字符串%s删除数字后为:", str);
  24. del_digit(str);
  25. return 0;
  26. }

练习11-9

  1. #include <stdio.h>
  2. #include <string.h>
  3. void str_renull(char* ptr)
  4. {
  5. while (*ptr)
  6. {
  7. *ptr = '\0';
  8. ptr++;
  9. }
  10. }
  11. int main()
  12. {
  13. char str1[128];
  14. char str2[128];
  15. char temp[128];
  16. int n;
  17. printf("输入字符串str1 = "); scanf("%s", str1);
  18. printf("输入字符串str2 = "); scanf("%s", str2);
  19. printf("n = "); scanf("%d", &n);
  20. printf("str1长度为%d。\n", strlen(str1));
  21. printf("str2长度为%d。\n", strlen(str2));
  22. printf("temp复制str1(all) = \"%s\"。\n", strcpy(temp, str1));
  23. str_renull(temp);
  24. printf("temp复制str2(%d) = \"%s\"。\n", n, strncpy(temp, str2, n));
  25. str_renull(temp);
  26. strcpy(temp, str1);
  27. printf("str1(all) & str2(all) = \"%s\"。\n", strcat(str1, str2));
  28. strcpy(str1, temp);
  29. printf("str1 & str2(%d) = \"%s\"。\n", n, strncat(str1, str2, n));
  30. strcpy(str1, temp);
  31. printf("str1(all) - str2(all) = %d。\n", strcmp(str1, str2));
  32. printf("str1(%d) - str2(%d) = %d。\n", n, n, strncmp(str1, str2, n));
  33. return 0;
  34. }

练习11-10

  1. //编写函数,实现atoi()、atol()、atof()三个函数的功能
  2. #include <stdio.h>
  3. #include <limits.h>
  4. int strtoi(const char* nptr)
  5. {
  6. int result = 0;
  7. int sign = 1;
  8. if (*nptr == '-')
  9. {
  10. sign = -1;
  11. nptr++;
  12. }
  13. if (*nptr == '+')
  14. {
  15. nptr++;
  16. }
  17. while (*nptr)
  18. {
  19. result = result * 10 + (*nptr) - '0';
  20. nptr++;
  21. }
  22. return result * sign;
  23. }
  24. long strtol(const char* nptr)
  25. {
  26. long result = 0;
  27. int sign = 1;
  28. if (*nptr == '-')
  29. {
  30. sign = -1;
  31. nptr++;
  32. }
  33. if (*nptr == '+')
  34. {
  35. nptr++;
  36. }
  37. while (*nptr)
  38. {
  39. result = result * 10 + (*nptr) - '0';
  40. nptr++;
  41. }
  42. return result * sign;
  43. }
  44. double strtof(const char* nptr)
  45. {
  46. double result = 0.0;
  47. double n = 1.0;
  48. int sign = 1;
  49. if (*nptr == '-')
  50. {
  51. sign = -1;
  52. nptr++;
  53. }
  54. if (*nptr == '+')
  55. {
  56. nptr++;
  57. }
  58. while (*nptr)
  59. {
  60. if (*nptr == '.')
  61. {
  62. nptr++;
  63. break;
  64. }
  65. result = result * 10 + (*nptr) - '0';
  66. nptr++;
  67. }
  68. while (*nptr)
  69. {
  70. n /= 10.0;
  71. result = result + ((*nptr) - '0') * n;
  72. nptr++;
  73. }
  74. return result * sign;
  75. }
  76. int main()
  77. {
  78. char str_int[128];
  79. char str_long[128];
  80. char str_double[128];
  81. printf("整形str_int = "); scanf("%s", str_int);
  82. printf("长整型str_long = "); scanf("%s", str_long);
  83. printf("双精度浮点型str_double = "); scanf("%s", str_double);
  84. printf("转换之后\n");
  85. printf("\"%s\" => int型 = %d。\n", str_int, strtoi(str_int));
  86. printf("\"%s\" => long型 = %ld。\n", str_long, strtol(str_long));
  87. printf("\"%s\" => double型 = %.10lf。\n", str_double, strtof(str_double));
  88. return 0;
  89. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/pnstu/article/detail/62779
推荐阅读
相关标签
  

闽ICP备14008679号