K& R练习1-19:反向字符阵列

时间:2016-08-21 22:34:27

标签: c arrays terminate

我能够正确地反转数组,但是当我在终端中执行CTRL + D(EOF)时,我无法终止程序。
我可以让程序终止的唯一方法是在编译后我做的第一件事是做CTRL + D.但是如果我键入一个字符串,那么CTRL + D将无效。

我不太确定我的错误在哪里。

#include <stdio.h>
#define MAXLINE 1000 // Maximum input.

// ----------------- reverseLine -----------------
// This method reads in chars to be put into an
// array to make a string. EOF and \n are the
// delimiters on the chars, then \0 is the
// delimiter for the string itself. Then the
// array is swapped in place to give the reverse
// of the string.
//------------------------------------------------
int reverseLine(char s[], int lim)
{
  int c, i, newL;
  // c is the individual chars, and i is for indices of the array.
  for (i = 0; i < lim - 1 && (c=getchar()) != EOF && c != '\n'; ++i)
    {
    s[i] = c;
    }

  if (c == '\n') // This lets me know if the text ended in a new line.
    {
      newL = 1;
    }

  // REVERSE
  int toSwap;
  int end = i-1;
  int begin = 0;
  while(begin <= end) // Swap the array in place starting from both ends.
    {
      toSwap = s[begin];
      s[begin] = s[end];
      s[end] = toSwap;

      --end;
      ++begin;
    }

  if (newL == 1) // Add the new line if it's there.
    {
      s[i] = '\n';
      ++i;
    }

  s[i] = '\0'; // Terminate the string.  

  return i;
}

int main()
{
  int len;
  char line[MAXLINE];

  while ((len = reverseLine(line, MAXLINE)) > 0)  // If len is  zero, then there is no line to recored.
    {
      printf("%s", line);
    }

  return 0;
}

我唯一能想到的是主要检查中的while循环,如果len&gt; 0,所以如果我输入EOF,也许它无法进行有效的比较?但是,当这是我输入的第一个也是唯一的东西时,为什么它起作用是没有意义的。

1 个答案:

答案 0 :(得分:0)

由于这种情况,您的程序永远不会读取EOF:

(c=getchar()) != EOF && c != '\n';

一旦c等于&#39; \ n&#39;循环终止,所有以下字符都被忽略。我认为你应该将输入与换行分开,并对反向函数参数进行常规检查。

 #include <stdbool.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>

 #define SIZE_MAX (256U)

 static bool linereverse(char *line);
 static bool deletenewline(char *s);

 int main(void)
 {
       char buff[SIZE_MAX];
       bool success;
       (void) fputs("Enter a string: ", stdout);
       if( NULL == fgets(buff,(size_t) SIZE_MAX, stdin))
       {
            (void) fputs("Error: invalid input!\n",stderr);
            return EXIT_FAILURE;
       }
       success = deletenewline(buff);
       if(false == success)
       {
             (void) fputs("Error: cannot remove newline\n",stderr);
             return EXIT_FAILURE;
       }
       success = linereverse(buff);
       if(false == success)
       {
            (void) fputs("Error: cannot reverse the line");
            return EXIT_FAILURE;
       }
      (void) fputs("The line reversed is: ", stdout);
      (void) fputs(buff, stdout);
      (void) puchar('\n');
      return EXIT_SUCCESS;
 }
 static bool linereverse(char *line)
 {
      size_t i;
      size_t j;
      char tmp;
      if(NULL == line)
      {
           return false;
      }
      i = 0;
      j = strlen(line) - 1;
     while(i < j)
     {
         tmp = line[i];
        line[i] = line[j];
        line[j] tmp;
        ++i;
         --j;
     }
     return true;
 }
static bool deletenewline(char *s)
{
     char *p;
     if(NULL == s)
     {
         return false;
     }
     p = strrchr(s,'\n');
     if(NULL != p)
     {
         *p = '\0';
     }
     return true;
}