2014-03-04 125 views
2

我的程序應該測試迴文,然後將其反向打印出來,但沒有像'!','或'?'這樣的字符。因此,輸入的「夫人我是亞當」輸入「madamimadam」,沒有大寫字母,空格或標點符號。我能夠編寫這個程序,但你怎麼做另一部分去除這些字符/大寫?同樣對於我的數組,當程序運行時,它會在打印輸出時在迴文和迴文之間輸出一組奇數字符。我相信這是因爲數組正在填充額外的字符空間,所以我如何解決這個問題呢?如何使用數組刪除特定字符的迴文?

#include<iostream> 
#include<string> 
using namespace std; 

int main() 
{ 
    //Variables and arrays 
    int const index = 30; 
    char Phrase[index]; 
    char Reverse[index]; 
    char* Palindrome = Reverse; 
    int i, j; 

    cout << "Please enter a sentence to be tested as a palindrome: "; 
    cin.getline(Phrase, 30); 
    int length = strlen(Phrase); 

    bool test = true; 

    for(i = 0; i != length/2; i++) //Loops from zero to half of the string 
    { 
     if(test) // if it is a palindrome so far 
     { 
      if(Phrase[i] != Phrase[length-i-1]) //To check if the characters match 
      { 
       test = false; 
      } 

     } 
     else 
     { 
      break; 
     } 
    } 

    if(test) 
    { 
     cout << endl << "Phrase/Word is a Palindrome." << endl << endl; 
     for(j = strlen(Phrase) - 1; j >= 0; Palindrome++, j--) 
     { 
      *Palindrome = Phrase[j]; 
     } 
     cout << "The phrase and reverse statement is: " << Reverse << endl << endl; 
    } 
    else 
    { 
     cout << endl << "Phrase/Word is not a Palindrome." << endl << endl; 
    } 

    system("Pause"); 
    return 0; 
} 
+0

您是否已經逐步調試程序以查看真正發生了什麼? –

+0

是的,我有,它通過所有的測試正常運行,但我不知道如何擺脫數組輸入的額外字符爲空。用戶應該能夠輸入一個體面長度的迴文,所以我把數組設置爲30 –

+0

迴文是相同的事情不是嗎?特意從不考慮的字符中劃分出來。 – imreal

回答

0
#include <iostream> 
#include <string> 
#include <cctype> 
    using namespace std; 

int main() 
{ 
//Variables and arrays 
int const index = 80; 
char Phrase[index]; 
char NewPhrase[index]; 
int i, j, k, l; 
bool test = true; 

//Prompt user for the phrase/word 
cout << "Please enter a sentence to be tested as a palindrome: "; 
cin.getline(Phrase, 80); 

//Make everything lowercase, delete spaces, and copy that to a new array 'NewPhrase' 
for(k = 0, l = 0; k <= strlen(Phrase); k++) 
{ 
    if((Phrase[k] != ' ') && (ispunct(Phrase[k]) == false)) 
    { 
     NewPhrase[l] = tolower(Phrase[k]); 
     l++; 
    } 
} 

int length = strlen(NewPhrase); //Get the length of the phrase 

for(i = 0, j = length-1; i < j; i++, j--) 
{ 
    if(test) //Test to see if the phrase is a palindrome 
    { 
     if(NewPhrase[i] != NewPhrase[j]) 
      test = false; 
    } 
    else 
     break; 
} 

if(test) 
{ 
    cout << endl << "Phrase/Word is a Palindrome." << endl << endl; 
    cout << "The Palindrome is: " << NewPhrase << endl << endl; 
} 
else 
    cout << endl << "Phrase/Word is not a Palindrome." << endl << endl; 

system("Pause"); 
return 0; 
} 
0

你忘了終止\0添加到Reverse。循環後添加*Palindrome = '\0'

+0

@ T-Bird請不要改正你的問題。 – Nabla

0

只使用單獨的迭代器。取而代之的

for(i = 0; i != length/2; i++) 

for(i = 0, j = length-1; i < j; i++, j--) 

然後你if語句,你可以這樣做

if(test) // if it is a palindrome so far 
{ 
    while(!isalpha(Phrase[i]) && i < j) { i++; } 
    while(!isalpha(Phrase[j]) && i < j) { j--; } 
    if(Phrase[i] != Phrase[j]) //To check if the characters match 
    { 
     test = false; 
    } 

,這將導致你的程序忽略除ISN任何字符不是信。如果你想讓它識別數字,你也可以使用isalphanum。

+0

我試過這個,但是它在第一個循環的時候給了我一個錯誤 –

+0

再試一次,我忘了關閉括號。 –

+0

它正確地反轉了一切,它忽略了那些字符,但它並沒有將它們從它中刪除 –

相關問題