hey guys! i am new to c++ and i need help with the following code. we are suppose to write a program and check if a word is palindrome or not.

#include <iostream>
using namespace std;

int IsPalindrome(int []);


void main()
{
	char word[25];

	cout <<" Enter a word: ";
	cin>> word;

	if (IsPalindrome(word)==1)
		cout<<" Is a palindrome ";
	else
		cout<<" Is not a palindrome "
			<<"\n";

	system("pause");
}	

int IsPalindrome(char str1 [])
{
	int i,counter=0;

	for(i=0; str1[i]!='\0'; i++);

	for(i--; i>=0; i--) 
		if (str1[i-1]==str1[i])
			counter++;
			
	return counter;
		
}

Recommended Answers

All 5 Replies

You'll have to take a look at your second loop and consider adding a second index (perhaps called 'j'). After that, take another look at how you are incrementing/decrementing your indexes.

You will then want to look at how you are doing your comparison. If I'm reading your code correctly, as it stands, your program currently will only call any "word" that has (1) (and only 1) pair of matching adjacent characters a palindrome even if the rest of the word is not. In addition, it will most likely miss many true palindromes. Run your program and try the palindromic word "tenet". It will say that it is not a palindrome because there are no adjacent matching characters.

so i will need a second index called j and compare the first half of the string (say i) with the second half (say j) to check if they match..i ll give it a try..actually when i compile the existing code i get this error:

"error C2664: 'IsPalindrome' : cannot convert parameter 1 from 'char [25]' to 'int []'

Hello!
This worked for me!

#include <iostream>
using namespace std;

int IsPalindrome(char*);
void main()
{
char word[25];

cout <<" Enter a word: ";

cin>> word;

     if (IsPalindrome(word))

      cout<<" Is a palindrome ";

      else

      cout<<" Is not a palindrome "

      <<"\n";

system("pause");

}


int IsPalindrome(char* str1)
{

int i,counter=0;

for(i=0; str1[i]!='\0'; i++);
counter=i-1;    //counter will contain the length of the array

for(i--; i>counter/2; i--)
    if (str1[i]!=str1[counter-i])      //verify if the letters match
        return false;    

return true;
}

@OP:
That is correct, start at the ends and work your way toward the middle. If you get a comparison that doesn't match, it should fail immediately.

[edit]
@silvy:
It is generally frowned upon (and against forum rules) to just hand out code to cut/paste. It is a hindrance to the learning process. That is why I made suggestions, and did not provide a corrected program.

What you call "counter" would replace what I was calling 'j'. But the way you did it is inefficient and redundant since 'i' already has the length of the word. You would use 'i' as the index of the end element, then you would initialize 'j' to (0) and use that as the index of the beginning element. In actuality, the use of the function strlen() is even better, then you don't even need the first loop at all.

thank you very much for your time its working now!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.