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

   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;


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

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


		while(!(isalnum(s1[j]))) j--;		//previous character
		if(j<0) break;				//break if it exceeds the array size

	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?


6 Years
Discussion Span
Last Post by urjapandya

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 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)



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);

// 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 by tesuji: typos, cout replaced by printf

void main()
      char str[50];
      int len=0,i,j,flag=1;
      printf("Enter the string: ");
      if (flag==1)
              printf("String is a Palindrome");
              printf("String is not a Palindrome");


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 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 by urjapandya: n/a

This article has been dead for over six months. Start a new discussion instead.
Be sure to adhere to our posting rules.