write a program to determine whether a string is palindrome or not
using pointer
using string functions
without using string functions
Asked in/Preparing for : Accenture


2 Responds



USING STRING FUNCTIONS
 
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
   char ch[20],ch1[20];
   clrscr();
   printf("Enter the Stringn");
   gets(ch);
   strcpy(ch1,ch);
   strrev(ch1);
   if((strcmp(ch,ch1))==0)
  {
      printf("Palindromen");
  }
  else
  {
      printf("Not Palindromen");
   }
   getch();
}
 
WITH OUT USING STRING FUNCTIONS
#include<stdio.h>
#include<conio.h>
void main()
{
    int i,count=0,j,temp=0;
    char ch[20],ch1[20];
    clrscr();
    printf("Enter the stringn");
    gets(ch);
    for(i=0;ch[i]!='0';i++)
    {
         count++;
    }
    for(i=count-1,j=0;i>=0;i--,j++)
    {
         ch1[j]=ch[i];
    }
    for(i=0;i<count;i++)
    {
        if(ch[i]!=ch1[i])
        {
              temp=1;
              break;
         }
    }
    if(temp==1)
    {
        printf("Not Palindromen");
    }
    else
    {
        printf("Palindromen");
    }
    getch();
}
 
USING POINTERS
#include<stdio.h>
#include<conio.h>
int palindrome(char s[50],int,int);
void main()
{
    char a[50],b[50];
    int l;
    clrscr();
    printf("Enter the stringn");
    gets(a);
    strcpy(b,a);
    l=strlen(b);
    palindrome(a,0,l-1);
    printf("Reverse is :");
    puts(a);
    if(strcmp(a,b))
   {
       printf("String is not a palindromen");
   }
   else
   {
       printf("string is palindromen");
    }
    getch();
}

int palindrome(char s[50],int beg,int end)
{
    char p;
    if(beg>=end)
    {
        return 0;
    }
    else
   {
       p=*(s+beg);
       *(s+beg)=*(s+end);
       *(s+end)=p;
       return palindrome(s,++beg,--end);
    }
}
Lokesh Batra
Reputation : 166



In this program, we use the fact that, If a string is palindrome then leftmost character of the string is equal to the rightmost character of the string. We initialize two integer variables to point to first and last characters of string. Inside while loop we compare left and right characters, if mismatch found then not a palindrome otherwise palindrome string.
/*
* C Program to check given string is pallindrome or not
*/
#include <stdio.h>
#include <conio.h>
#include <string.h>
int main(){
  char inputString[100];
  int leftIndex, rightIndex, length = 0;
  printf("Enter a string for palindrome check\n");
  scanf("%s", inputString);
  /* Find length of input string */
  while(inputString[length] != '\0')
     length++;
  /* If length of string is less than 1, ERROR */
  if(length < 1)
    return 1;
   /* Initialize leftIndex and rightIndex to first and
   last character of input string */
   leftIndex = 0;
   rightIndex = length -1;
   /* Compare left and right characters, If equal then
   continue otherwise not a palindrome */
   while(leftIndex < rightIndex){
      if(inputString[leftIndex] != inputString[rightIndex]){
         printf("%s is not a Palindrome \n", inputString);
         return 0;
      }
     leftIndex++;
     rightIndex--;
   }
   printf("%s is a Palindrome \n", inputString);
   getch();
   return 0;
}
Source : C program to check string is palindrome
Akbar Khan
Reputation : 1



Written
Exam Questions
BLOG Posts
For study materials
Job Walk-In
Updates across India
Interview round
Questions
Interview or procedure
Experiences
Files
For study materials
Please LIKE our page to Get regular JOB WALK-IN UPDATES across India and STUDY MATERIALS on facebook news feed.