Write a function that checks whether a string of characters is palindrome or not.

Write a function that checks whether a string

Data Structure / Assignment 4 / Set B

The User-defined header file:

#include<stdio.h>
#include<string.h>
#define MAX 15
char name[MAX],top;
void init()
{
top=-1;
printf("\nStatic Stack Initiaized\n");
}
void push(char c)
{
top++;
name[top]=c;
}
char pop()
{
return name[top--];
}
isempty()
{
if(top==-1)
{
printf("\nStatic Stack is empty\n");
}
else
{
printf("\nStatic Stack is not empty\n");
}
}

Save this above code in the stack library file as cststack.h

void palindrome();
#include<stdio.h>
#include<stdlib.h>
#include "cstack.h"
int main()
{
int ch,i;
char string[15];
while(1)
{
printf("\n-----Static Stack to check palindrome string-----\n");
printf("\n1.Init\n");
printf("2.Check Palindrome String\n");
printf("3.IsEmpty\n");
printf("4.Exit\n");
printf("Enter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1:init();  break;
case 2: palindrome(); break;
case 3:isempty(); break;
case 4:exit(0);
}
}
}
void palindrome()
{
char string[15];
int i,count=0,len;
printf("\nEnter a string:");
scanf("%s",string);
len=strlen(string);
for(i=0;i<len;i++)
{
push(string[i]); 
}	
for (i = 0; i <len; i++)
{
if (string[i] == pop())
count++;
}
if (count == len)
printf("%s is a Palindrome string\n", string);
else
printf("%s is not a palindrome string\n", string);
}

Save this file as stack.c and compile and run the stack.c

Output:
-----Static Stack to check palindrome string-----
1.Init
2.Check Palindrome String
3.IsEmpty
4.Exit
Enter your choice: 1
Static Stack Initiaized
-----Static Stack to check palindrome string-----
1.Init
2.Check Palindrome String
3.IsEmpty
4.Exit
Enter your choice: 2
Enter a string:123423
123423 is not a palindrome string
-----Static Stack to check palindrome string-----
1.Init
2.Check Palindrome String
3.IsEmpty
4.Exit
Enter your choice: 2
Enter a string:121
121 is a Palindrome string
-----Static Stack to check palindrome string-----
1.Init
2.Check Palindrome String
3.IsEmpty
4.Exit
Enter your choice: 2
Enter a string:malayalam
malayalam is a Palindrome string
-----Static Stack to check palindrome string-----
1.Init
2.Check Palindrome String
3.IsEmpty
4.Exit
Enter your choice: 3
Static Stack is empty
-----Static Stack to check palindrome string-----
1.Init
2.Check Palindrome String
3.IsEmpty
4.Exit
Enter your choice: 4

By Admin

Leave a Reply

Your email address will not be published. Required fields are marked *