Determines whether or not it is a palindrome

  • Thread starter Hadhod
  • Start date
  • #1
16
0
I don't 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
What would you do, if someone handed you a word for testing?
 
  • #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 ??please
 
Last edited:
  • #5
Ok
Iwill show you my answer soon
thank you for helping me
 
  • #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
please help me
 
  • #7
please I need a help
 
  • #8
This should help you, sorry for the delay i was busy with finals!. Just one thing it won't work if the text line has spaces. Any questions about the code, post them here. :)

Code:
#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
}
 
  • #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
 
  • #10
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

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:
  • #11
Thank you very much
 
  • #12
PDA is the most elegnet way to program a palindrome. you don't need to use a counter and temp variables for it

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.



 
  • #13
Here's one I did recursively in Java sometime ago.

Code:
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-");
			}
		}		
	}
}
 

Suggested for: Determines whether or not it is a palindrome

Replies
6
Views
726
Replies
5
Views
1K
Replies
1
Views
619
Replies
4
Views
815
Replies
16
Views
620
Replies
10
Views
1K
Replies
2
Views
524
Replies
9
Views
774
Replies
1
Views
663
Back
Top