2016-08-21 53 views
1

我可以反轉數組,但是我無法讓程序在終端執行CTRL + D(EOF)時終止。
我可以讓程序終止的唯一方法是如果編譯後我做的第一件事是做CTRL + D。但是如果我輸入一個字符串,那麼在此之後CTRL + D將不起作用。K&R練習1-19:反向字符數組

我不太確定我的錯誤在哪裏。

#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> 0,所以如果我鍵入EOF,也許它不能進行有效的比較?但是,當這是我輸入的第一個也是唯一的東西時,它爲什麼會起作用沒有任何意義。

+0

你不是在Windows上運行你的程序,是嗎? – dasblinkenlight

+0

@ user3121023你是對的,它就這麼簡單。我很尷尬,我沒有嘗試做幾次。謝謝。 – boy

+1

您不初始化'newL',所以如果您的讀取循環沒有以換行符結束,您的程序會顯示未定義的行爲。 – rici

回答

0

你的程序將永遠不會讀取EOF的,因爲這種情況:

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

只要c等於「\ n」循環終止與以下所有字符都被忽略。我認爲你應該將輸入與換行分開,並對反向功能參數進行常規檢查。

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