2016-04-28 49 views
0

我剛剛編譯這個程序,但它似乎不工作。使用C反向字符串比較使用C

我應該做的是制定一個程序,確定一個單詞是否是迴文(前後相同的單詞,例如「賽車」或「眼睛」)。

它也應該忽略大小寫字母(例如Racecar或eYe)。這是我到目前爲止有:

int main() 

    char word[21],reverse[21]; 
    printf("Type a word and I will tell you if it is a Palindrome: "); 
    fgets(word,21,stdin); 
    puts(""); 
    printf("The word that you typed is: %s \n",word); 
    strcpy(reverse,word); 
    strrev(reverse); 
    if((strcmp(reverse,word))==0) 
    printf("This word is a palindrome"); 
    else 
    printf("This word is NOT a palindrome"); 

    return 0; 
} 
+0

很酷,你的程序工作嗎? –

+7

那麼最新的問題?你提到似乎沒有工作,爲什麼呢?一個錯誤?錯誤的輸出? –

+1

「它不工作」 - 冠軍的問題描述 –

回答

1

fgets()存儲換行符到提供的緩衝區,如果遇到一個。當您輸入一個單詞並按回車鍵時,「輸入」會產生一個換行符,並將其存儲在緩衝區中。當你反轉字符串時,換行字符將成爲反轉的一部分,因此,你永遠不會得到肯定的結果。此外,爲了確保比較可以不區分大小寫,可以先將整個單詞轉換爲小寫(或大寫)。

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

int main() 
{ 
    char word[21] = {0}, reverse[21]; 

    printf("Type a word and I will tell you if it is a Palindrome: "); 
    fgets(word,21,stdin); 

    // remove linefeed character if there is one 
    size_t len = strlen(word); 
    if (len > 0 && word[len - 1] == '\n') 
    { 
     word[len - 1] = '\0'; 
     len--; 
    } 

    // convert string to lowercase 
    for (size_t i = 0; i < len; i++) 
     word[i] = tolower(word[i]); 

    puts(""); 
    printf("The word that you typed is: %s \n",word); 

    strcpy(reverse,word); 
    strrev(reverse); 

    if((strcmp(reverse,word))==0) 
     printf("This word is a palindrome"); 
    else 
     printf("This word is NOT a palindrome"); 

    return 0; 
} 
+0

測試'fgets()'的結果?我喜歡'word [strcspn(word,「\ n」)] ='\ 0';'作爲安全地刪除換行符的單行方式,如果它存在並用另一個空字節覆蓋空字節。它只假定'word'是一個以空字符結尾的字符串。 –

+0

我更喜歡'strlen'的方式。無論如何,你通常需要這個長度來達到其他目的。 – dreamlax

+0

在你需要這個長度的地方,POSIX ['getline()'](http://pubs.opengroup.org/onlinepubs/9699919799/functions/getline.html)函數開始計分,因爲它告訴你長度準確。但你說得對;如果你需要這個長度,那麼'strcspn()'不是一個好處。 –

3

你不需要字符串逆向檢查它是否是一個迴文,也strrev是不是在C標準的功能。您可以從單詞的開頭和結尾處進行檢查:

char word[21]; 
printf("Type a word and I will tell you if it is a Palindrome: "); 
fscanf(stdin, "%s", word); 
puts(""); 
printf("The word that you typed is: %s \n",word); 

size_t len = strlen(word); 
int s; 
bool palindrome = true; 
for (s = 0; s < len/2; s++) { 
    if (tolower(word[s]) != tolower(word[len-1-s])) { 
     palindrome = false; 
     break; 
    } 
} 
if(palindrome) 
    printf("This word is a palindrome"); 
else 
    printf("This word is NOT a palindrome"); 

這節省了空間和時間。

0
  • 試試這個簡單的代碼:
#include <stdio.h> 
#include <string.h> 

int main() 
{ 
    char word[21] = {0}, reverse[21] = {0}; 
    int len; 
    printf("Type a word and I will tell you if it is a Palindrome: "); 
    fgets(word,21,stdin); 

    len = strlen(word); 

    // convert string to lowercase and remove '\n' feed problem if any 
    for (int i = 0; i < len ; i++) 
    { 
    if(word[i] != '\n') 
    word[i] = tolower(word[i]); 
    } 
    word[i] = '\0'; 

    puts(""); 
    printf("The word that you typed is: %s \n",word); 

    strcpy(reverse,word); 
    strrev(reverse); 

    if((strcmp(reverse,word))==0) 
     printf("This word is a palindrome"); 
    else 
     printf("This word is NOT a palindrome"); 

    return 0; 
0

與fgets,也使\ n在字符串的結尾。 所以,正確的代碼將是這樣的:

int main() 
{ 

    char word[21],reverse[21]; 
    printf("Type a word and I will tell you if it is a Palindrome: "); 
    fgets(word,21,stdin); /// Actually it saves: "123\n" 
    word[strlen(word)-1] = '\0'; 
    puts(""); 
    printf("The word that you typed is: %s \n",word); 
    strcpy(reverse,word); 
    strrev(reverse); 
    if((strcmp(reverse,word))==0) 
    printf("This word is a palindrome"); 
    else 
    printf("This word is NOT a palindrome"); 

    return 0; 
}