Wednesday 12 September 2012

To determine if the given string is a palindrome or not


Description:
if the reverse of a string is equal to original string then it is called palindrome

Algorithm:

Step 1:start
Step 2: read the string
Step 3: store reverse of the given string in a temporary string
Step 4: compare the two strings
Step 5: if both are equal then print palindrome
Step 6: otherwise print not palindrome
Step 7: stop

 Program:
#include<stdio.h>
#include<string.h>

enum Boolean{false,true};
enum Boolean IsPalindrome(char string[])
{
            int left,right,len=strlen(string);
            enum Boolean matched=true;
 if(len==0)
  return 0;
  left=0;
            right=len-1;

             /* Compare the first and last letter,second & second last & so on */
             while(left<right&&matched)
  {
   if(string[left]!=string[right])
                         matched=false;
                         else
                        {
                                     left++;
                                    right--;
                        }
            }
  return matched;
 }

int main()
{
  char string[40];
  clrscr();
  printf("****Program to test if the given string is a palindrome****\n");
  printf("Enter a string:");
  scanf("%s",string);
  if(IsPalindrome(string))
  printf("The given string %s is a palindrome\n",string);
  else
  printf("The given string %s is not a palindrome\n",string);
  getch();
  return 0;
}


Output:
1. Enter the string:malayalam
     The given string malayalam is a palindrome
2. Enter the string:india
    The given string india is not a palindrome
Conclusion: The program is error free


VIVA QUESATIONS:

1)      What is meant by palindrome ?
Ans: If the reverse of a string/number is equal to original string/ number then it is  
called palindrome.

2)      What is the use of gets() function ?
Ans: To read the string at a time
3)       What is the use of puts() function ?
Ans: To write the string at a time






No comments:

Post a Comment