A program to check if the string is palindrome with and without using Stack | Using C language | Data structure |
🤔What is a palindrome?
- A palindrome is a word, number, phrase, or other sequences of characters that reads the same backward (from right to left) as forward (from left to right).
- For example: madam, 313, ABCCBA, 12321 etc.
👉Click here to Read about Stack and its push, pop and Other operations
👨💻Write a program to check if the string is palindrome using Stack.
CODE:
🔘With using Stack
//using Stack
#include<stdio.h>
#include<string.h>
main()
{
int tops1=-1,tops2=-1;
int i,stringlength,flag=0;
char S1[100],S2[100];
printf("Enter the string:");
gets(S2);
// strcpy, strrev, strlen, etc. are the function of string.h header file.
strcpy(S1,S2); //strcpy used for copy string S2 into string S1
strrev(S2); //strrev used for reverse string S2
stringlength=strlen(S1); //strlen used for calculate the length of string S1
tops1=stringlength-1;
tops2=stringlength-1;
for(i=0;i<stringlength;i++)
{
if(S1[tops1]==S2[tops2])
{
tops1--;
tops2--;
}
}
if(tops1==-1 && tops2==-1)
{
printf("string is palindrom");
}
else
{
printf("string is not palindrom !!!");
}
}
👨💻Write a program to check if the string is palindrome without using Stack.
CODE:
🔘Without using Stack
//without using Stack
#include<stdio.h>
#include<string.h>
main()
{
char S1[100] ,S2[100];
puts("Enter the string:");
gets(S2);
// strcpy, strrev, strlen, strcmp etc. are the function of string.h header file.
strcpy(S1,S2); //strcpy used for copy string S2 into string S1
strrev(S1); //strrev used for reverse string S2
if(strcmp(S1,S2)==0) //strcmp used for compare two strings S1 and S2
{
printf("string is palindrom");
}
else
{
printf("string is not palindrom !!");
}
}
(Both codes produce the same output)
OUTPUT:
Comments
Post a Comment