2015-10-06 76 views
0

我試圖找到一個在互聯網上的解決方案,但無法找到類似的東西。我使用strcpy和迭代在C++中創建迴文函數,所有工作都正常,但是strcpy部分。我不知道如何解決它或使用其他替代方法。謝謝。Palindrome C++(strcpy)

#include <iostream> 
#include <cstring> 

using namespace std; 

void palindrom(char[]); 

int main() 
{ 
    char binput[100]; 

    cout << "Hello please enter your word here: " << endl;  
    cin >> binput; 
    palindrom(binput); 

    system("pause"); 
    return 1; 
} 

void palindrom(char binput[]) 
{ 
    int max= strlen(binput); 
    char cinput[100]; 
    char dinput[100]; 

    for (int i=max, n=0; i>=0, n<=max; i--, n++) 
     strcpy(dinput[n],binput[i]); 

    cout << dinput << endl; 

    if (strcmp(binput,dinput)==true) 
     cout << "Is palindrome " << endl; 
    else 
     cout << "Is not " << endl; 
} 
+0

* 「我使用的strcpy」 * - 使用'的std :: string'。 –

+0

你需要它在C++或是Java好嗎? –

+0

@JürgenK。我需要它在C++謝謝 –

回答

0

希望這solves.Basically第一隻檢字和最後的第一個字母。如果它們不相等,那麼它們不是迴文。如果它們相同,則通過比較前端字符和它們各自的後端來繼續。

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

int CheckPalindrome(char input[],int len); 


int main() 
{ 
    char input[100]; 
    int result,inpLen; 


    cout<<"Enter Word:"<<endl; 
    cin>>input; 
    cout<<"Entered Word:"<<input<<endl; 
    cout<<"Checking....."<<endl; 
    inpLen=strlen(input); 
    result=CheckPalindrome(input,inpLen); 
    if(result == 1) 
    { 
    cout<<"Entered Word:"<<input<<" is a palindrome!"<<endl; 
    } 
    else 
    { 
    cout<<"Entered Word:"<<input<<" is not a palindrome!"<<endl; 
    } 

return 0; 
} 

int CheckPalindrome(char input[],int len) 
{ 

    int result; 

    if(input[0] != input[len-1]) 
    { 
     result = 0; 
    } 
    else 
    { 
    for(int i=0 ; i<len ; i++) 
    { 
    if(input[i] == input[len-1-i]) 
    { 
     result = 1; 
    } 
    else 
    { 
     result = 0; 
     break; 
    } 
    } 
    } 

return result; 
} 
+0

謝謝,這是一個非常不錯的選擇。 –

-1

你應該初始化我到MAX-1而不是最大,你擁有了它,現在它會將NULL終止字符「\ 0」複製到的dinput的第一要素,這導致在0長度字符串的方式。

您還需要確保NULL終止dinput。嘗試:

for (int i=max-1, n=0; i>=0, n<=max; i--, n++) 
    dinput[n] = binput[i]; 

dinput[max] = '\0'; 
+0

你有沒有試過編譯你的代碼? -1。 –

+0

是的,那個人在我身上。我遇到了我看到的第一個明顯的問題,並將其鎖定在該問題上。糾正工作代碼。 /嘆息 – Scott

+0

@Scott你說得對,謝謝。但是由於strcpy,代碼仍然沒有編譯。 –

0

看起來像你不清楚一個什麼strcpy做什麼。它將整個字符串從源複製到目標。你不需要這裏。你需要做簡單的任務。

假設您的輸入是​​。我假設你想從它創建字符串"abccba"

鑑於輸入的字符:

+---+---+---+ 
| a | b | c | 
+---+---+---+ 

你需要將它們作爲映射到輸出數組:

binput[0] 
|  binput[len-1] 
|  | binput[len-1] 
| .... | |  binput[0] 
|  | | .... | 
v  v v  v 
+---+---+---+---+---+---+ 
| a | b | c | c | b | a | 
+---+---+---+---+---+---+ 

現在,邏輯轉換成代碼:

int len= strlen(binput); 
char dinput[100]; 

for (int i = 0; i < len; ++i) 
{ 
    dinput[i] = binput[i];   // Takes care of the left side of the palindrome. 
    dinput[2*len-i-1] = binput[i]; // Takes care of the right side of the palindrome 
} 

// Make sure to null terminate the output array. 
dinput[2*len] = '\0'; 

更新,回覆OP的評論

您需要:

for (int i = 0; i < len; ++i) 
{ 
    dinput[len-i-1] = binput[i]; 
} 
dinput[len] = '\0'; 
+0

是的,我理解這裏的邏輯,功能非常強大,但我不需要「abccba」我需要dinput只有「cba」才能夠與後面的binput進行比較,並檢查它是否是迴文。幾乎在那裏謝謝你。 –

+0

@Alfiebrown,查看更新。 –

0
if(strcmp(word,strrev(word)==0) 

Pallindrome