2010-10-14 47 views
1

我試圖確定一個短語是否是一個迴文(從左到右相同的一個詞)或不,但我不能讓它工作。出了什麼問題?我無法使用指針或遞歸或字符串類型變量迴文沒有指針和遞歸

#include <stdio.h> 

#include <string.h> 

int main() 

{ 

int i,j = 0,length; 
char space = ' '; 
char phrase [80],phrase2[80],phrase3[80]; 

printf("Give me the phrase: "); 
gets(phrase); 
length = strlen(phrase); 

for(i =0; i <= length - 1; i++) 
{ 
    if(phrase[i] != space) //Makes the phrase without spaces 
    { 
    phrase2[i] = phrase[i]; 
    j++; 
    } 
} 

for(i = length -1; i >= 0;i--) 
{ 
    if(phrase[i] != space) //Makes the phrase backwards an without spaces 
    { 
    phrase3[j] = phrase[i]; 
    j++; 
    } 
} 

length = strlen(phrase2); 

for(i =0; i <= length -1;i++)  //Compare the phrases to know if they are the same 
{ 
    if(phrase2[i] != phrase3[i]) 
    { 
    printf("It's not a palindrome\n"); 
    return 0; 
    } 
} 
printf("It's a palindrome\n"); 
return 0; 
} 
+0

任何錯誤信息?你的樣本輸入和輸出是什麼? – BoltClock 2010-10-14 01:04:41

+1

你需要重置j – Anycorn 2010-10-14 01:06:22

+0

我總是得到「這不是迴文」的信息,我輸入的例子絕不會是奇數或偶數。短語2它應該得到neveroddoreven和短語3這個詞在後面沒有空格至少是一樣的neveroddoreven – Enrique 2010-10-14 01:09:51

回答

2

試試這個:

for(i =0, j=0; i <= length - 1; i++) 
{ 
    if(phrase[i] != space) //Makes the phrase without spaces 
    { 
    phrase2[j] = phrase[i]; 
    j++; 
    } 
} 

for(i = length -1, j = 0; i >= 0;i--) 
{ 
    if(phrase[i] != space) //Makes the phrase backwards an without spaces 
    { 
    phrase3[j] = phrase[i]; 
    j++; 
    } 
} 

length = j; 

更新

針對裁判官的職位,這裏的代碼做它不需要複製字符串。

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

int main() 
{ 
    int i, j, length; 
    char space = ' '; 
    char phrase[80]; 

    printf("Give me the phrase: "); 
    gets(phrase); 
    length  = strlen(phrase); 

    for(i = 0, j = length - 1; i < j; i++, j--) { 
    while (phrase[i] == space) i++; 
    while (phrase[j] == space) j--; 
    if(phrase[i] != phrase[j]) { 
     printf("It's not a palindrome\n"); 
     return 0; 
    } 
    } 

    printf("It's a palindrome\n"); 
    return 0; 
} 
1

在第二個循環之前,您要設置j = 0。它應該在那之後工作。 PS:如果你打印出你的三個字符串進行調試,你可能在幾分鐘內就知道了。當你不知道出了什麼問題時,在中間步驟打印出變量的值,以便知道問題發生在哪裏以及它是什麼。

1

您的問題已被其他人回答,但我發佈此代碼以表明不需要使phrase3副本保留反向字符串。

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

int main() 
{ 

    int i, j, length, halfLength; 
    char space = ' '; 
    char phrase1[80], phrase2[80]; 

    printf("Give me the phrase: "); 
    gets(phrase1); 
    length  = strlen(phrase1); 

    for(i = 0, j = 0; i <= length; ++i) { 
    if(phrase1[i] != space) { //Makes the phrase1 without spaces 
     phrase2[j++] = phrase1[i]; 
    } 
    } 

    length  = strlen(phrase2); 
    halfLength = length/2; 

    for(i = 0, j = length - 1; i < halfLength; ++i, --j) { 
    if(phrase2[i] != phrase2[j]) { 
     printf("It's not a palindrome\n"); 
     return 0; 
    } 
    } 

    printf("It's a palindrome\n"); 
    return 0; 
} 
+0

也可以在一個循環中完成,而不需要複製字符串。您只需檢查空格並在從每端解析字符串時適當調整索引。 – 2010-10-14 22:22:31

0

這是我想出了:

#include <stdio.h> 
void main() { 
char a[50],b[50]; 
int i=0,j,ele,test=0,x; 
while((a[i]=getchar())!='\n') { 
if(a[i]!=' ' && a[i]!=',') //do not read whitespaces and commas(for palindromes like "Ah, Satan sees Natasha") 
i++; 
} 
a[i]='\0'; 
ele=strlen(a); 
// Convert string to lower case (like reverse of Ava is avA and they're not equal) 
for(i=0; i<ele; i++) 
if(a[i]>='A'&&a[i]<='Z') 
a[i] = a[i]+('a'-'A'); 
x = ele-1; 
for(j=0; j<ele; j++) { 
b[j] = a[x]; 
x--; 
} 
for(i=0; i<ele; i++) 
if(a[i]==b[i]) 
test++; 
if(test==ele) 
printf("You entered a palindrome!"); 
else 
printf("That's not a palindrome!"); 
} 

可能不是迴文的最佳方式,但我很自豪我對我自己做這個花了我1小時:(笑

-1

爲什麼不使用std::stack?你需要兩個循環,每個循環迭代輸入字符串的長度,在第一個循環中,遍歷輸入字符串一次,將每個字符壓入堆棧中;在第二個循環中,彈出一個字符關閉堆棧並將其與索引中的字符進行比較。如果你得到了在循環結束之前不匹配,你沒有迴文。與此有關的好處是,你不必擔心偶數/奇數長度的角落情況。它會工作。 (如果您非常喜歡,您可以使用一個堆棧(LIFO)和一個隊列(FIFO),但這並不會顯着改變算法)。

這裏的實現:

bool palindrome(const char *s) 
{ 
    std::stack<char> p; // be sure to #include <stack> 

    for(int i = 0; s[i] != 0; i++) 
     p.push(s[i]); 

    for(int i = 0; s[i] != 0; i++) 
    { 
     if(p.top() != s[i]) 
      return false; // not a palindrome! 

     p.pop(); 
    }  

    return true; 
} 

跳過空格就留給讀者做練習;)