簡體   English   中英

使用scanf的C中的回文,並且沒有字符串庫函數

[英]Palindrome in C using scanf and no string library functions

分配是獲取輸入字符串,並且不使用任何字符串庫函數來處理該字符串。 此刻此代碼甚至無法打印出我輸入的字符串。當我從main中刪除函數時,它神奇地開始打印。 任何幫助將不勝感激

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

#define SIZE 32

int isQuit(char str[]);
void isPalindrome(char str[]);


int main (){
    int cont = 0;
   char str[SIZE];
   fflush(stdin);
   printf("please enter a word:\n");
   scanf("%s\n", str);
   printf("%s\n", str);

  while(cont == 0)
  {
    scanf("%s\n", str);
   printf("%s\n", str);
     cont =  isQuit(str);
    isPalindrome(str);
  }
   return 0;
}

您很可能正遭受終端中行緩沖的困擾。 在編寫換行符之前,不會顯示任何寫入的字符。

在顯示輸入內容時,嘗試添加換行符:

printf("%s\n", str);

您要確保顯示的任何其他printf調用也是如此。

順便說一句,您的零終止測試不正確。 轉義字符是\\ ,而不是/ 將循環更改為:

while (str[h] != '\0')

或者簡單地:

while (str[h])

您的代碼在這里有些錯誤:

while(isQuit(str) == 0)
{ 
    isPalindrome(str);
    return 0 ;
}

由於您的循環主體中有return關鍵字(無條件),因此循環最多執行一次。

同樣, isQuitisPalindrome都不從用戶isPalindrome獲取輸入。 這意味着,即使你通過刪除固定循環return聲明,但它仍然是不對的; 您將有一個無限循環的isQuitisPalindrome通過第15行上用戶要求的相同str傳遞。

除了@paddy的答案中指出的問題外,您要做的就是更改while循環,以不斷輪詢用戶的輸入並對其進行操作。

暫無
暫無

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

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