2014-11-23 68 views
0

我試圖寫一個程序,讀取文件,並檢查它是否是迴文(雙方同一個詞)發現一個字或詞,如果是這樣,它們被保存到由返回分隔的另一個文件中。閱讀文件中的單詞可以用任何方式書寫:用空格隔開,用句子或回車隔開。查找單詞的文件,檢查它們是否迴文

#include<stdlib.h> 
#include<stdio.h> 
#include<string.h> 
#define MAX 255 

int palindrome(char *x, int y, int i) 
{ 
    while(i<=y){ 
     if(x[i]!=x[y]) 
      return 0; 
     i++;y--; 
    } 
    return 1; 
} 

int main() 
{ 
    char *reading; 
    int length; 
    int x=0; 
    int y=0; 
    char read[MAX]; 
    char write[MAX]; 
    FILE *r; 
    FILE *w; 
    puts("Enter read file name"); 
    scanf("%s", read); 
    puts("Enter write file name"); 
    scanf("%s", write); 
    r=fopen(read, "r"); 
    if(r==NULL) 
     perror("File does not exist"); 
    w=fopen(write, "w"); 
    reading=malloc(MAX*sizeof(char)); 
    while(fgets(reading, MAX, r)!=NULL) 
    { 
     length=strlen(reading); 
     while(x<=length){ 
      for(x=y; ;x++){ 
       printf("%c\n", reading[x]); 
       if((reading[x]>='a'&& reading[x]<='z') || (reading[x]>='A' && reading[x]<='Z')) 
        break; 
      } 
      for(y=x; ;y++){ 
       printf("%c\n",reading[y]); 
       if((reading[y]>='a'&& reading[y]<='z') || (reading[y]>='A' && reading[y]<='Z')); 
       else 
        break; 
      } 
      if(palindrome(reading, y, x)==1) 
       for(;x<=y;x++) 
       fputc(reading[x], w); 
      x=y; 
     } 
    } 
    fclose(r); 
    fclose(w); 
    return 0; 
} 

問題是代碼不工作,如何解決它?

+0

是否張貼的代碼工作? – 2014-11-23 10:50:48

+4

那麼問題到底是什麼? – Mureinik 2014-11-23 10:57:28

+0

問題是代碼不起作用... – DevGambit 2014-11-23 11:57:10

回答

0

我在你的代碼有誤,這裏用錯限制x

length=strlen(reading); 
while(x<=length){ 

所以我做的方式相當多的變化probem被解決,而不是找出你可能會打破什麼其他限制。我已將輸出寫入stdout而不是寫入文件。請注意,我還在文件打開模式中添加了"t"

#include <stdio.h> 
#include <stdlib.h> 
#include <ctype.h> 

#define MAX 255 

int palindrome(char *string, int x, int y) 
{ 
    if (x >= y) 
     return 0; 
    while (y > x) 
     if (tolower(string[x++]) != tolower(string[--y])) 
      return 0; 
    return 1; 
} 

int main() 
{ 
    char reading[MAX+1]; 
    char readfile[MAX+1]; 
    int x, y, i; 
    FILE *r; 
    puts("Enter read file name"); 
    scanf("%s", readfile); 
    r=fopen(readfile, "rt"); 
    if(r==NULL) 
     perror("File does not exist"); 
    else { 
     while (fgets (reading, MAX, r) != NULL) { 
      x = 0; 
      do { 
       while (reading[x] && !isalpha (reading[x])) 
        x++; 
       y = x; 
       while (isalpha (reading[y])) 
        y++; 
       if (palindrome (reading, x, y)) { 
        printf ("Palindrome: "); 
        for (i=x; i<y; i++) 
         printf ("%c", reading[i]); 
        printf ("\n"); 
       } 
       x = y; 
      } 
      while (reading[x]); 
     } 
     fclose(r); 
    } 
    return 0; 
} 
+0

謝謝,這是相當豐富的,一個問題,「rt」和「r」有什麼區別? – DevGambit 2014-11-23 13:31:08

+0

文件開啓默認爲''b「'二進制模式,您正在讀寫文本文件。 – 2014-11-23 13:44:54

+0

我不能完全理解的另一件事是這些行確實是這樣做的:while(reading [x] &&!isalpha(reading [x])) - 爲什麼需要閱讀[x]和!isalpha是不夠的? while(isalpha(reading [y])) - 認爲這意味着如果字母是來自字母表,那麼循環繼續////// while(reading [x]); - 什麼時候會是假的? ////// – DevGambit 2014-11-23 21:47:22

相關問題