write a c program to check given string is palindrome or not | CTechnotips


Definition of Palindrome string: 

A string is called palindrome if it symmetric. In other word a string is called palindrome if string remains same if its characters are reversed. For example: asdsa
If we will reverse it will remain same i.e. asdsa

Example of string palindrome:  a,b, aa,aba,madam,qwertrewq etc.

Code 1.

#include<string.h>
#include<stdio.h>
int main(){
  char *str,*rev;
  int i,j;
  printf("\nEnter a string:");
  scanf("%s",str);
  for(i=strlen(str)-1,j=0;i>=0;i--,j++)
      rev[j]=str[i];
      rev[j]='\0';
  if(strcmp(rev,str))
      printf("\nThe string is not a palindrome");
  else
      printf("\nThe string is a palindrome");
  return 0;
}

Code 2.

#include <stdio.h>
#include <conio.h>
#include<string.h>
#define max 50

void main()
{
    int length,i,j,flag=0;
    char str[max];
    clrscr();
    printf("\n Enter String : - ");
    scanf("%s",str);
    length=strlen(str);
    for(i=0,j=length-1;i<(length/2);i++,j--)
    {
        if(str[i]!=str[j])
        {
            flag=1;
            break;
        }
    }
    if(flag==1)
        printf("\n%s String is not Palindrom",str);
    else
        printf("\n%s String is Palindrom",str);
    getch();


Code 3.

#include<stdio.h>
int main(){
  char str[100];
  int i=0,j=-1,flag=0;
  printf("Enter a string: ");
  scanf("%s",str);
  while(str[++j]!='\0');
  j--;
  while(i<j)
      if(str[i++] != str[j--]){
           flag=1;
           break;
      }
     
  if(flag == 0)
      printf("The string is a palindrome");
  else
      printf("The string is not a palindrome");
  return 0;
}

1 comments:

Unknown said...
This comment has been removed by the author.

Post a Comment