Community discussion forum

Checking a palindrome using arrays

  • 1 year ago

    Good day guys, Im having problems finding a palindrome using this code, hope somebody could lend me a hand, thanks a lot!

    Here's my code:

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

    int main () {
      char str[20];
      char rev[20];
      int i,z=0; 
      cout << "Please enter full name: ";
      cin.getline (str,20);
      i = strlen(str);
      for (z=0; z>=i; z++)
      {
         // Help here to get palindrome... thanks!
         revPerson= str[i-z];
         cout << "here" << revPerson;
      }

      return 0;
    }

  • 1 year ago

    If you want to check 'str' is a palindrome, here is a way :

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

    int main()
    {
        char str[7] = "123321",
            rev[7];

        strcpy(rev, str); // copy 'str' into 'rev'
        strrev(rev); // reversing 'rev'

        if (strcmp(str, rev) == 0)
            cout << "It's palindrome";

        return 0;
    }

  • 1 year ago

     Mohammad Rastkar, thanks for the reply... The code runs in windows platform but the it does not work in a g++ compiler in ubuntu? The strrev is not recognized.  Is there any other way?

     I have a modifed code that gets the length and swap places to show reversal of text. The only problem is that what would be the statement for analyzing each character and then tells if it is a palindrome or not. Hope you could still help me... Thanks in advance!

    Here's my code:

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

    int main () {
      char str[20];
      char rev[20];
      int cc=0;
      int i,z=0;  
      cout << "Please enter a word: ";
      cin.getline (str,20);
      i = strlen(str);
      // places reverse string
      for (z=0;z<=i;z++)
      {
        revPerson = str[i-z];
      }  
      // displays original string
     cout << "Original message " ;
     for (z=0;z<=i;z++)
      {
         cout << strPerson;
      }
      cout << endl;
      // displays reversed string
     cout << "Reversed message " ;
      for (z=0;z<=i;z++)
      {
         cout << revPerson;  
      }

      // I guess there should be if statements here
      cout << endl;
       return 0;
    }

     

  • 1 year ago

    Then use the following code snippet :

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

    int main()
    {
        char str[10] = "123404321";
        int len = strlen(str);

        int    mid = len/2, // middle of the string
            end = len-1;

        bool is_palindrome = true;

        for (int i = 0; i != mid; i++)
        {
            if (str[i] != str[end-i])
                is_palindrome = false; // not palindrome
        }

        cout << (is_palindrome ? "It's palindrome." : "it's not palindrome.") << endl;

        return 0;
    }

Post a reply

Enter your message below

Sign in or Join us (it's free).

We'd love to hear what you think! Submit ideas or give us feedback