Checking a palindrome using arrays

  • 9 years 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;
    }

  • 9 years 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;
    }

  • 9 years 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;
    }

     

  • 9 years 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).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Anyone who considers arithmetic methods of producing random digits is, of course, in a state of sin.” - John von Neumann