簡體   English   中英

EOF 和 GETCHAR 結束循環

[英]EOF AND GETCHAR to end loop

我是 C 編程語言的新手。 我有一個問題,如何在 Windows 中結束循環。

#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、用 EOF 替換 '\\n' 等等。 沒有什么對我有用。 我在 Windows 10 上使用 CodeBlocks。除了 getchar 和 eof 之外,還有其他方法可以編寫此類程序嗎?

你可能想再看看這個部分

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

我想這不是你想要的。

在該循環中,您需要再次讀取下一個字符,因此需要在該循環中添加getchar()

  c=getchar();
  while(c!= '\n')
  {
  i[d]=c;
  d++;
  c=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;
}

而不是使用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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM