2015-12-14 65 views
-4

我任務是創建一個程序,將檢查輸入的字符串是迴文或不使用字符串函數,所以我有這個簡單的程序在這裏:如何檢查是否字符串是迴文沒有C++

#include <iostream> 
#include <stdlib.h> 
#include <string.h> 
using namespace std; 

int main() 
{ 
    char y[100], x[100]; 
    cout << "Enter word" << endl; 
    cin >> y; 
    strcpy(x, y); 
    strrev(x); 
    if (strcmp(y, x) == 0) 
     cout << "Palindrome"; 
    else 
     cout << "Not Palindrome"; 
    system("pause"); 
    return 0; 
} 

但是,我不允許使用像strcpy,strrev,strcmp等字符串函數。在這種情況下,我可能會爲此程序使用一組字符。如果代碼很容易理解,我可能會感激,因爲我是C++的初學者。任何幫助表示讚賞。

***感謝您的幫助,我完成了大部分程序。

***我忘了添加,程序忽略字符串中的空格,比如「rad ar」或「race car」仍然會作爲迴文返回。遺憾的是,我無法弄清楚這種空間檢查功能的編碼。

+3

要解決您的問題,請考慮在迴文中,第一個字符等於最後一個字符,第二個字符與第二個字符相等,依此類推。你應該可以用'for'循環很容易地測試它。 –

回答

-1

這裏亞去:

#include <iostream> 
#include <stdlib.h> 
using namespace std; 

int main(){ 
    char y[100], x[100]; 
    cout<<"Enter word"<<endl; 
    cin>>y; 

    //Get the size of the word entered 
    int len = 0; 
    char*p = y; 
    while(*p++) len++; 

    //Check for palindrome 
    bool palindrome=true; 
    for(int i=0; i<len/2; ++i){ 
      if(y[i]!=y[len-1-i]) palindrome=false; 
    } 
    cout << "palindrome:" << (palindrome?"true":"false") << "\n"; 
    return 0; 
} 
+0

Downvoted作爲代碼唯一的答案,沒有解釋OP做錯了什麼。另外OP指定一個空間必須工作,而你的代碼不會處理。 – Tas

1

迴文就是它的第一個字符是等於最後一個,所以就一個字。因此,爲了檢查它是否是迴文,你只需要複製strlen函數的功能來知道你必須比較第一個字符的字符的位置。

在C++中,用你的變量,這可以很容易地用while循環完成:

int i = 0; 
// Is this a null terminating character? 
while (y[i]) 
{ 
    // Move to the next character 
    i++; 
} 

爲了使事情變得更加容易,真正成爲一個下拉函數strlen,這可以把一個函數:

int stringLength (char *input) 
{ 
    int i = 0; 
    while (input[i]) 
    { 
     i++; 
    } 
    return i; 
} 

現在,你只需要遍歷輸入,第一個字符比較到最後,第二個字符比較倒數第二個,依此類推......你一定要記住的是,由於way數組工作,最後一個字符實際上在len-1位置。

#include <iostream> // required for cout, and cin 

// returns the length of a c style string 
int stringLength(char *input) 
{ 

    int i = 0; 
    // Is this a null terminating character 
    while (input[i]) 
    { 
     // No, check the next character 
     i++; 
    } 
    return i; 
} 

int main() 
{ 
    // Get input from the user. 
    char input[100]; 
    std::cin >> input; 

    // Calculate the length of the input 
    int length = stringLength(input); 

    // At position length is the null terminating character, 
    // the last character is actually at position len - 1 
    int lastIndex = length - 1; 

    // Stores whether of not we found a palindrome 
    bool isPalindrome = true; 

    // Loop through the string checking if the first character is equal to 
    // the last, second to second last etc... 
    for (int i = lastIndex; i >= length/2; i--) 
    { 
     // Check the palindrome condition 
     if (input[i] != input[lastIndex - i]) 
     { 
      isPalindrome = false; 
      break; 
     } 
    } 

    // Output the result 
    if (isPalindrome) 
    { 
     std::cout << "Palindrome" << std::endl; 
    } 
    else 
    { 
     std::cout << "Not palindrome" << std::endl; 
    } 

    return 0; 
} 
+0

如果你不想檢查每個角色兩次,爲什麼不中途停止? –

+0

出於某種原因,我認爲如果我這樣做會有一個邊界案例會失敗。我測試了它,它的工作原理,謝謝! – hargasinski