EOF和GETCHAR结束循环

问题描述:

我是C编程语言的新手。我有一个问题,我怎样才能结束循环在Windows中。EOF和GETCHAR结束循环

#include<stdio.h> 
#include<conio.h> 

int main() { 
    printf("enter ur palindrome"); 
    int i[100], c, d = 0, n = 0; 

    c = getchar(); 
    while (c != '\n') { 
     i[d] = c; 
     d++; 
    } 
loop: 
    while (d != 0) { 
     if ((i[0 + n] != i[d - n])) { 
      n++; 
      goto loop; 
     } 

     printf("this is not a palindrome"); 

     break; 
    } 
    printf("this is a palindrome"); 

    return (0); 
} 

我已经试过几乎所有CTRL + Z,CTRL + C,CTRL + d,替换 '\ n' WITH EOF ,还有更多的事情。没有为我工作。我在Windows 10上使用CodeBlocks。 是否有其他方式来编写除getchar和eof以外的其他类型的程序。

+0

使用。 –

+0

@ ameyCU while循环不终止 –

+0

@ Atomic_alarm if + retrun怎么办?可以üplzz解释 –

而不是使用goto不同的方式,使用continue;来重复循环。

但是,发布代码还存在其他一些问题。

The array `int i[100]` is never terminated with a NUL byte ('\0') 
An array of char not int should be used. 
this loop: `while (d != 0) will never exit, 
    because (if the loop is ever entered) 
    the `d` variable is never changed within the loop 

这里是我的推荐代码:

警告:如果+回报不彻底的测试

#include <stdio.h> 
//#include<conio.h> <-- not used, so do not include 
#include <string.h> 

#define MAX_LENGTH (100) 

int main(void) 
{ 
    int d; 
    int n; 
    char i[MAX_LENGTH]; 

    printf("enter ur palindrome\n"); // <-- \n so will immediately print 

    if (NULL != fgets(i, MAX_LENGTH, stdin)) 
    { 
     if(strlen("\n") < strlen(i)) 
     { // then, some string entered 

      // remove any trailing '\n' 
      char *newline = strstr(i, "\n"); 
      if(newline) 
      { // then '\n' found 
       *newline = '\0'; 
      } // end if 

      d = strlen(i); 
      for(n=0; (d-n) >= n; n++) 
      { 

       if(i[0 + n] != i[d - n]) 
       { // then no match 
        printf("this is not a palindrome"); 
        break; 
       } // end if 
      } // end for 

      if((d-n) < n) 
      { // then palindrome 
       printf("this is a palindrome"); 
      } // end if 
     } 

     else 
     { 
      printf("nothing entered\n"); 
     } // end if 
    } // end if 

    return (0); 
} // end function: main 

你可能想看看在本节再次

c=getchar(); 
    while(c!= '\n') // Value of c never altered, hence it'll never break 
    { i[d]=c; 
    d++; 
    } 

,是的,其他环

loop: // not required 
    while ( d!=0 ) // can't see change in d within this body, is it n ? 
    { 
    if((i[0+n] != i[d-n])) 
    { 
     n++; 
     goto loop; // not required 
     continue; //will do 
    } 
    printf("this is not a palindrome"); 
    break; 
} 

和你实际上得到一个额外的消息说

this is a palindrome 

印刷后

this is a not palindrome 

我想这不是你想要的。

+0

ameycu我是新来的:) :) –

+0

@azamiftikhar是的,我很感谢你的努力,但我的观点是在编码之前仔细考虑算法,这样编码后你将不得不做更少的修改。对不起,如果听起来很刺耳,我不是故意的。 – ameyCU

+0

感谢ameycu和kkk对你宝贵的意见 –

在这种循环中,您需要再次读取下一个字符因此需要在循环

c=getchar(); 
    while(c!= '\n') 
    { 
    i[d]=c; 
    d++; 
    c=getchar(); 
    } 

这里补充getchar()是编写一段代码

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


int main() 
{ 
    char *word; 
    int i; 
    int n; 

    printf("enter ur palindrome"); 
    scanf("%[^\n]", word);      // reads input all the way to the new line 
    n = strlen(word); 

    for (i = 0; i < n; i++) { 
     if (word[ i ] != word[ n-1 - i ]) { 
      break; /* for */ 
     } 
    } 
    if (i == n) { 
     printf("This is a palindrome"); 
    } else { 
     printf("This is not a palindrome"); 
    } 

    return 0; 
} 
+0

1)'word'没有字符内存 - 这是一个未初始化的指针! 2)'scanf(“%[^ \ n]”,word);'如果输入以'\ n''开头,'不能将任何东西扫描到'word'中。 – chux