I am trying to write a funtion that will return the value of 1 if its string argument is a palindrome.

I have started but am unsure if it is correct. Could someone please take a look and point me in the right direction?

Thanks

#include <string.h>

Int palindrome (const char *s)
{
	int I, l;
	l = strlen (s);
	for (i=0; i<1/2; i++)
	{
		If (s[i] != s[l-i-1]) return 0;
	}
	Return 1;
}

Recommended Answers

All 5 Replies

Number one, I have to ask, how strict is your definition of palindrome?

Would you consider this one - Madam, I'm Adam

Where punctuation, case and spaces are ignored.

I would consider that a palindrome...punctuation may be ignored.

I am trying to write a funtion that will return the value of 1 if its string argument is a palindrome.

I have started but am unsure if it is correct. Could someone please take a look and point me in the right direction?

Thanks

#include <string.h>

Int palindrome (const char *s)
{
	int I, l;
	l = strlen (s);
	for (i=0; i<1/2; i++)
	{
		If (s[i] != s[l-i-1]) return 0;
	}
	Return 1;
}

It looks like you wrote this in a word processor rather than a code editor; C is case-sensitive. And perhaps you might be interested in more meaningful variable names, because it seems that you have confused the letter ell and the number one.

But if you can decipher your own algorithm in that, it's generally a good start. Ignoring punctuation, spacing, and letter case would need a bit more finesse.

I never done a palindrome routine before but I would:

1. take original string and create new string removing all characters like spaces, tabs, punctuation.

2. I would take the new string and check each character with either toupper/tolower
therefore nullifying case.

These are only suggestions because like I said I never create a palindrome routine.

First make a palindrome that is case and punctuation sensitive :

You code revised a little :

#include <string.h>

bool palindrome (const char *s)
{
	int index = 0, length = 0;
	length = strlen (s);
        int halfLen = length/2;
	for (index = 0 ;  index  < halfLen;  i++){
		If (s[index] != s[length- index-1]) 
                   return false;
	}
	return true;
}

Now you need to first add a couple of things to it.
1) Makes the sentence all lower or all upper case
2) Deletes punctuation from the string
3) and add other restriction.

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.