简体   繁体   中英

Palindrome C++ (strcpy)

I tried to find a solution on internet but couldnt find anything similar to this. I am using strcpy and iteration to make a palindrome function in c++ everything is working fine but the strcpy section. I dont know how to solve it or what other alternative to use. Thank you.

#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;
}

Looks like you are not clear one what strcpy does. It copies an entire string from a source to a destination. You don't need that here. You need to make simple assignments.

Let's say your input is "abc" . I assume you want to create the string "abccba" from it.

Given the characters in the input:

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

you need to map them to the output array as:

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

Now, translate that logic into code:

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';

Update, in response to OP's comment

You need:

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

Hope this solves.Basically first just check the first letter of the word and the last. If they are not equal then they are not palindrome. If they are equal then proceed on by comparing from the front end character with their respective back ends.

#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;
}
if(strcmp(word,strrev(word)==0)

回廊

You should initialize i to max-1 instead of max, the way you have it now it will copy the NULL terminator character '\\0' to the first element of dinput which results in a 0 length string.

You will also need to make sure to NULL terminate dinput. Try:

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

dinput[max] = '\0';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM