Jimmy Chen

A Programmer

C Primer Plus 第六版 第十一章课后编程练习答案

编程环境Visual Studio 2017

此文为博主原创文章,转载请注明出处

  1. 郭冇说道:

    第五题不是应该返回指针吗? :confused:

  2. ch说道:

    第7题应该是复制到s1字符串而不是s1的末尾吧? s1[s1_length + i] = s2[i] 这里

    1. jimmychen说道:

      书不在身边,不知道题目具体要求是什么?看我自己写的程序貌似是将两个字符串拼接起来,如果题目要求是将两个字符串拼接起来的话,那上面的程序是没问题的,就是函数名起得不好。如果不是将两个字符串拼接的话,那上面的程序就写错了。

      1. ch说道:

        要求是把第二个字符串拷贝到第一个是类似strncpy不是strncat拼接。

        1. jimmychen说道:

          谢谢,已经改回来了

  3. 友人A说道:

    第二道题要求是如果遇到空白、制表符、换行符时也能停止。被调函数读输入的时候好像是完成了这个要求,但是主调函数打印还是num个单位,调试时空白字符后面几位的数据打印成了莫名其妙的东西。所以要怎么改,我的想法是从被调函数传递空白字符的位置给主调函数循环打印,但是好像改错了...,还是不行。

  4. sunrise说道:

    楼主 我这是根据11题,写出的“按照长度递增顺序打印字符串”,但是运行之后不按照顺序打印,感觉写的没问题,不知道哪里出了问题,楼主能给看下么
    #define _CRT_SECURE_NO_WARNINGS
    #include
    #include
    #include
    #define LIM 10
    #define SIZE 256
    void func3(char*st[], int num);//给字符串排序的函数
    char*s_gets(char*st, int n);//读入字符串的函数
    int main(void)
    {
    int i = 0;
    char input[LIM][SIZE];
    char*ptr3[LIM];

    while (i < LIM && s_gets(input[i], SIZE))
    {
    ptr3[i] = input[i];//这样操作,不会改变原有数组
    i++;
    }
    func3(ptr3, i);//函数调用
    for (int n = 0; n < i; n++)//打印排序好的字符串
    puts(ptr3[n]);

    return 0;
    }

    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;
    }

    void func3(char*st[], int num)
    {
    char*ct;
    int x, y;

    for (x = 0; x < num - 1; x++)
    {
    for (y = x+1; y 0)
    {
    ct = st[x];
    st[x] = st[y];
    st[y] = ct;
    }
    }
    }
    }

    1. sunrise说道:

      上面的程序复制的有问题 是下面这个
      #define _CRT_SECURE_NO_WARNINGS
      #include
      #include
      #include
      #define LIM 10
      #define SIZE 256
      void func3(char*st[], int num);//给字符串排序的函数
      char*s_gets(char*st, int n);//读入字符串的函数
      int main(void)
      {
      int i = 0;
      char input[LIM][SIZE];
      char*ptr3[LIM];

      while (i < LIM && s_gets(input[i], SIZE))
      {
      ptr3[i] = input[i];//这样操作,不会改变原有数组
      i++;
      }
      func3(ptr3, i);//函数调用
      for (int n = 0; n < i; n++)//打印排序好的字符串
      puts(ptr3[n]);

      return 0;
      }

      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;
      }

      void func3(char*st[], int num)
      {
      char*ct;
      int x, y;

      for (x = 0; x < num - 1; x++)
      {
      for (y = x+1; y 0)
      {
      ct = st[x];
      st[x] = st[y];
      st[y] = ct;
      }
      }
      }
      }

  5. aiguo说道:

    第十六题,第一个if 为啥加上这个条件argc == 1,它是什么作用;为啥三个if里面都用的是argv[1],这题不是有三个命令行参数(-p,-u,-l),我感觉是argv【1】,argv【2】,argv【3】

  6. 海桥说道:

    最后一条 我执行了 为什么会无缘无故退出啊??还有第14题编译之后有一个警告!

  7. 厚脸皮黑帅说道:

    我的第5题(感觉比较简洁):
    char * strchr2(char * st1,char * st2)
    {
    int num = strlen(st2);
    int i;
    while(*st1++)
    {
    for(i = 0;i < num;i++)
    {
    if(st1[i] != st2[i])
    break;
    if(i == num-1)
    return st1;
    }
    }
    return NULL;
    }

回复 厚脸皮黑帅 取消回复

您的电子邮箱地址不会被公开。 必填项已用*标注