I have made a program to test if the string entered is a palindrome or not. Here's the source code:

#include <stdio.h>
#include <ctype.h>
#include <string.h>
void clrsp(char *);
void rev(const char *,char *);

int main()
{
   char str1[80],str2[80];

   printf("Enter a string: ");
   fgets(str1,80,stdin);

   str1[strlen(str1)-1]='\0';			//eliminate newline

   clrsp(str1);				//clear spaces and punctuation marks
   rev(str1,str2);			        //reverse the string

   /*Use strcmp to check if two strings are same*/
   if(strcmp(str1,str2)==0) printf("%s is a palindrome!\n",str1);
   else printf("%s is not a palindrome!\n",str1);

   return 0;
}

/*Clear spaces and punctuations marks*/
void clrsp(char *s)
{
	int i=0,j=0;
	int n;

	n=strlen(s);

	while(i<n)
	{
		while(!(isalnum(s[j]))) j++;		//next character
		if(j>=n) break;				//break if it exceeds the array size
		s[i]=s[j];
		i++;
		j++;
	}
	s[i]='\0';               			//append terminating character
}

/*Reverse string*/
void rev(const char *s1,char *s2)
{
	int i=0,j;
	int n;

	n=strlen(s1);
	j=n-1;

	while(i<n)
	{
		while(!(isalnum(s1[j]))) j--;		//previous character
		if(j<0) break;				//break if it exceeds the array size
		s2[i]=s1[j];
		i++;
		j--;
	}

	s2[i]='\0';				        //append terminating character
}

The program clears all the spaces and punctuation marks using clrsp().
It then reverse the string using rev().
Finally it uses strcmp() to check if two strings are same.

First of all, I would like to know how can I improve the palindrome program. Am I doing it the stupid way? Are there any better ways?

Second, how can I make a program that uses a recursive function to identify palindromes? I do not even have the faintest idea on how to achieve this?

Thanks!

You don't mention the most important part of your program - does it work correctly?

Everything else is secondary.

Assuming it does work properly, then what deficiency do you notice or what is it that you want to improve?

With a palindrome program, if it's accurate with all kinds of input, you usually have no worries about the run-time, amount of memory it uses, or anything else. It's a small job, and runs in the blink of an eye.

Say you found a way to optimize the run time so it ran twice as fast - no one would notice or care, unless they strangely had a stop watch and very fast reflexes, right? You don't want to spend time highly optimizing something that can give you no real benefit from.

I thought the overall algorithm was fine, but your posting was not so good. You used tabs in the code you posted, instead of the spaces option, and the comments ran WAY off the line they were intended for. An indentation of 2-4 spaces, is perfect, please use it. Keep the comments either on the line they refer to, or just above it, always.

Edited 6 Years Ago by Adak: n/a

Everything works fine in program.

Thanks for the advice. I'll improve my posting and ask more meaningful questions. (anyway,I'm just a newbie poster:P)

Hello

I was also able to find the answer to Napoleon Bonaparte most impotant statement by means of this code:

int palindrom(char s[]){
int i,l;for(l=0;s[l]!='\0';l++);
for(i=0;i<l/2;i++) if(s[i]!=s[l-i-1])return(0);
return(1);}

// usage int main():
   if (palindrom("able was I ere I saw elba")) printf("Napoleon was right!\n");
     else printf("Wrong, wasn't he banned to Santa Helena!\n");

-- tesu

Edited 6 Years Ago by tesuji: typos, cout replaced by printf

#include<stdio.h>
#include<conio.h>
void main()
{
      char str[50];
      int len=0,i,j,flag=1;
      clrscr();
      printf("Enter the string: ");
      scanf("%s",str);
      while(str[i++]!='\0')
      {
              len++;
      }
      for(i=0,j=(len-1);i<len/2;i++,j--)
      {
              if(str[j]!=str[i])
              {
                       flag=0;
                       break;
              }
      }
      if (flag==1)
              printf("String is a Palindrome");
      else
              printf("String is not a Palindrome");
      getch();
}

Hi....

Palindrome is the word or a phrase which reads the same if we read it backward or forward. eg:- Madam backward is madaM. this is palindrome

Try this program this is easy and nice to understand....

In this program there is a variable name str where the user will enter the string name. Then to know the length of the string I have used while loop when i can know the length of the string. Through for loop we can rev the string as well as we can compare the string so that we can come to know where the the reverse string is equivalent to the main string. In for loop i is the variable where the string is straight and j is the variable through which we can reverse the string in that loop i have kept one condition when the reverse string and the main string is compared if the condition is false even once the value of variable flag will be 0 and then the loop will be stop as there is break. Last i have kept one condition where there is a flag if the value of flag is 1 then the string is Palindrome else not.
I hope this will help u.

Edited 3 Years Ago by Reverend Jim: Fixed formatting

Welcome to the forum, Urjapandya!

Two things:

1) Your program is the same as the program suggested just above your post (Tesu), (but yours is more readable).

2) It would be MUCH better if you had surrounded your code in CODE tags, which makes the forum software keep it looking like a program.

Just click on the [code] symbol, and then paste your code between the two tags that it puts on the editor window for you.

You and Tesu both, have found the more efficient algorithm for detecting palindrome's. Good job! ;)

Thanks Adak for the suggestion i will surely keep that things in mind. I have tried to write in code but at that time i was not able to post the thread as there was some error coming so i didn't use the code and i directly posted my solution. I am new user to this website so i was not knowing. Again Thanks for the Help Adak.

Edited 6 Years Ago by urjapandya: n/a

This article has been dead for over six months. Start a new discussion instead.