2015-10-06 69 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