Determines whether or not it is a palindrome

  • Thread starter Thread starter Hadhod
  • Start date Start date
Click For Summary

Discussion Overview

The discussion revolves around creating a function, named palindrome(), to determine if a given line of text is a palindrome. Participants explore various programming approaches, including C++ implementations and theoretical concepts like pushdown automata (PDA), while addressing issues such as handling whitespace, punctuation, and case sensitivity.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant requests help in building a program to check for palindromes, specifying the need to ignore whitespace, punctuation, and capitalization.
  • Another participant suggests checking characters from the beginning and end of the string towards the center, using the example of the word "madam" as a palindrome.
  • A participant emphasizes the importance of showing prior effort to receive help, referencing forum guidelines.
  • A C++ code snippet is shared by one participant, which attempts to check for palindromes but is noted to not work correctly.
  • Another participant provides an alternative C++ implementation but points out that it won't handle spaces correctly.
  • One participant proposes using a pushdown automaton (PDA) as an elegant solution for checking palindromes, describing a method involving a stack to compare characters.
  • Another participant agrees with the PDA approach but mentions the need for a stack implementation, questioning the necessity of counters and temporary variables in the process.
  • A participant shares a recursive Java implementation of a palindrome checker, detailing how it processes input and checks for palindromes while ignoring punctuation and spaces.

Areas of Agreement / Disagreement

Participants express various methods for checking palindromes, with some favoring simple character comparison in C++ and others advocating for more complex approaches like PDA. There is no clear consensus on the best method, and the discussion remains open to different perspectives.

Contextual Notes

Some implementations discussed may not handle all edge cases, such as ignoring spaces and punctuation, and there are unresolved issues regarding the efficiency and correctness of the proposed solutions.

Hadhod
Messages
16
Reaction score
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
 
Physics news on Phys.org
What would you do, if someone handed you a word for testing?
 
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:
Ok
Iwill show you my answer soon
thank you for helping me
 
#include<iostream>
#include<cstring>
using namespace std;
int main()
{//declaration
const int size=10;
char str[size];
//promot the user to enter a text
count<<"Please,enter a line of text: "<<endl;
//read the text
cin.get(str,size);

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


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




return 0;
}
this is my answer
but it does not work
please help me
 
please I need a help
 
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
}
 
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
Tacomablack said:
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
Tacomablack said:
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-");
			}
		}		
	}
}
 

Similar threads

Replies
55
Views
7K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 12 ·
Replies
12
Views
10K
  • · Replies 8 ·
Replies
8
Views
7K
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
0
Views
2K
  • · Replies 3 ·
Replies
3
Views
8K
  • · Replies 21 ·
Replies
21
Views
3K
  • · Replies 4 ·
Replies
4
Views
5K