1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Determines whether or not it is a palindrome

  1. May 10, 2008 #1
    I dont no how to build this program?

    Write a function, palindrome(), that processes a line of text and determines whether or not it is a palindrome. Ignore white space, punctuation, and capitalization. You may assume that each line of text is less than 100 characters.


    Plz help me
     
  2. jcsd
  3. May 10, 2008 #2

    robphy

    User Avatar
    Science Advisor
    Homework Helper
    Gold Member

    What would you do, if someone handed you a word for testing?
     
  4. May 10, 2008 #3
    I will check the first letir and the last letir
    then check the second letir and befor the last letir
    like madam
    madam is palindrome
    so
    can you help me ??plz
     
    Last edited: May 10, 2008
  5. May 10, 2008 #4

    robphy

    User Avatar
    Science Advisor
    Homework Helper
    Gold Member

  6. May 10, 2008 #5
    Ok
    Iwill show you my answer soon
    thank you for helping me
     
  7. May 11, 2008 #6
    #include<iostream>
    #include<cstring>
    using namespace std;
    int main()
    {//declaration
    const int size=10;
    char str[size];
    //promot the user to enter a text
    cout<<"Please,enter a line of text: "<<endl;
    //read the text
    cin.get(str,size);

    for(int n=0; n<size; n++)
    cout<<str[n];//print it on tne screen


    for(int i=0; i<size ;i++)
    {
    if(str==str[size-1-i]) //checking
    {
    cout<<" is a palindrome. "<<endl;
    break;
    }
    if(str!=str[size-1-i])
    {
    cout<<" is not a palindrome. "<<endl;
    break;
    }
    }




    return 0;
    }
    this is my answer
    but it does not work
    plz help me
     
  8. May 11, 2008 #7
    plz I need a help
     
  9. May 25, 2008 #8
    This should help you, sorry for the delay i was busy with finals!. Just one thing it wont work if the text line has spaces. Any questions about the code, post them here. :)

    Code (Text):

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

    void palindrome (char palindrome [100]);

    int main()
    {
        //Declarations.
        const int size = 100;
        char palindromestring[size];

        //Promt the user to enter a line of text.
        cout << "Welcome to the palindrome identifier.\n";
        cout << "Please, enter a line of text: ";

        cin >> palindromestring;   //Read the line of text.

        cout << endl << endl;  // Makes the output more clean.

        palindrome (palindromestring);
       
        return 0;

    }

    void palindrome (char palindromestring[100])
    {
        //Temp variables
        int n, i, counter = 0;

        //Validation begins
            for (n = 0; palindromestring[n] != '\0'; n++)  //This instruction outputs the values of the string until the value is '\0'
            {
                cout << palindromestring[n]; //Output Begins
            }

            for (i = 0; i < n; i++)  // Loops run from 0 to the length of the string.
            {
                if (palindromestring[i] == palindromestring [n-i-1])  // Comparation of values begins.
                {
                    counter++; // If the above condition is true, counter gets incremented by 1.
                }
            }

            if (counter == n) //If the total of counter equals the length (n) of the string, means the string is a palindrome.
            {
                cout << " is a palindrome.\n\n";
            }
            else
                if (counter != n) //Else counter and length (n) are not similar.
                {
                    cout << " is not a palindrome.\n\n";
                }
        //Validation Ends
    }
     
     
  10. May 26, 2008 #9
    if you want to implement is different than cannibal did you can make a PDA(pushdown Automata)

    first thing you need to do is push something one the stack like # or ? then you push all the characters on the stack until you get to the middle. Once you get to the middle you pop a charater you compare it with the character your at. When it pops the # or ? of the stack than that means your done and that you have a palindrome. If your comparisons returns a false then that means that it is not a palindrome.

    PDA is the most elegnet way to program a palindrome. you don't need to use a counter and temp variables for it
     
  11. May 26, 2008 #10
    Yeah that is another way of doing it too. I just wanted him to see it in simple C++, since that was the king of solution he try to archive.
     
    Last edited: May 26, 2008
  12. May 26, 2008 #11
    Thank you very much
     
  13. May 26, 2008 #12

    Borek

    User Avatar

    Staff: Mentor

    That's assuming you don't have to implement the stack. If the stack is (somehow, somewhere) implemented it already contains (could be hiden from you) counter (or pointer to the top), and stack memory serves as a temp variable.

    Hadhod: "is a palindrome" is a final message, that can be displayed once all characters have been tested, you are displaying it in each loop run. And you don't have to loop through size characters - size/2 will be enough, as you are testing two characters in each step.



     
  14. Jun 6, 2008 #13
    Here's one I did recursively in Java sometime ago.

    Code (Text):

    class PalindromeTester
    {
        public static void main(String[]args)
        {
            String word, run = "y";
            Scanner scan = new Scanner(System.in);
           
            while ((run.substring(0, 1)).equals("y")) //Program keeps running while the input starts with 'y' and halts if it starts with anything else
            {
                System.out.println();
                System.out.print("Enter a potential palindrome: ");
                word = scan.nextLine();
                System.out.println(); //scans the inputted palindrome then assigns it to a variable and passes it to the paltest() method
                paltest(word);
                System.out.println();
                System.out.print("Test another palindrome (y/n)? ");
                run = scan.nextLine();
                while (run.length() == 0) //Error checking in case no input was entered for 'run' in the outer loop, prevents StringIndexOutOfBounds
                {
                    System.out.println();
                    System.out.println("*Please enter yes or no*");
                    System.out.println();
                    System.out.print("Test another palindrome (y/n)? ");
                    run = scan.nextLine();
                }
                run = run.toLowerCase(); //sets the input for 'run' to lower case so the program will keep running on input of 'y' regardless of case
            }
       
        }
       
        public static void paltest(String word)
        {
            int left, right;
           
            word = word.toLowerCase(); //sets the argument passed from main method to lower case so case is ignored in evaluation and assigns it to local variable 'word'
           
            word = word.replaceAll(",", ""); //deletes all instances of punctuation and spaces in the string
            word = word.replaceAll("[.]", "");
            word = word.replaceAll("\'", "");
            word = word.replaceAll(";", "");
            word = word.replaceAll("\"", "");
            word = word.replaceAll("\\(", "");
            word = word.replaceAll("\\)", "");
            word = word.replaceAll("\\[", "");
            word = word.replaceAll("\\]", "");
            word = word.replaceAll("\\{", "");
            word = word.replaceAll("\\}", "");
            word = word.replaceAll(" ", "");
           
            left = 0;
            right = word.length();
           
            if (word.length() <= 1) //Checks to see if the word is long enough to be a palindrome and whether to evaluate the string or not
            {
                System.out.println("Please enter a word that is two or more characters long");
            }
           
            else if (word.length() <= 3) //base case, if the word length is at least 3, then it can be considered for palindrome checking
            {
                if (word.charAt(left) == word.charAt(right-1)) //Checks the first and last characters of the string if they're equal,
                    System.out.println("-This IS a palindrome-"); //it is a palindrome if they are, the middle character is irrelevant in a string of 3 characters
                else
                    System.out.println("-This is NOT a palindrome-");
            }  
       
            else
            {   //if the word is longer than 3 characters, check the first and last character to see if its equal
                if (word.charAt(left) == word.charAt(right-1))
                {   //if it is equal, cut off the first and last characters of the string and call the paltest() method again with the new shorter string
                    left += 1;
                    right -= 1;
                    word = word.substring(left, right);
                    paltest(word); //keep calling paltest() with a new string with the first and last characters removed until it reaches the base case or until
                }                   //the first and last characters are not equal
               
                else //if the word is longer than 3 characters and the first and last characters are not equal
                {
                    System.out.println("-This is NOT a palindrome-");
                }
            }      
        }
    }
     
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?