C Programming – HackerRank Solution | Palindrome Stack String |

(HackerRank) Write a Modular C Programming code to solve Palindrome Stack String, Help Micky to perform reversing and also to check whether a string is palindrome.

Micky is very fond of stacks data structure. He started experimenting the usage of stack for different purposes. He knew how to reverse a string while learning C programming. Now, the same task he wants to achieve using Stacks.

Help Micky to perform reversing and also to check whether a string is palindrome.

Input Format

first line reads the string.

Constraints

input data is string only.

Output Format

print the string is palindrome or not.

Sample Input 0

GADAG

Sample Output 0

GADAG is a palindrome string

Sample Input 1

HUBBALLI

Sample Output 1

HUBBALLI is not a palindrome string

Refer : C Programming HackerRank all solutions for Loops | Arrays | strings | Data Structures | Linked lists | Stacks | Queues | Binary Trees

 

CODE:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#define SIZE 100

struct stack
{
    char data[SIZE];
    int top;
};
void push(struct stack *sptr, char num);
void check(struct stack *sptr,char no[SIZE]);
char pop(struct stack *sptr);

void push(struct stack *sptr, char num)
{
    if(sptr->top==SIZE-1)
    {
        printf("Stack Overflow\n");
    }
    else
    {
        sptr->top++;
        sptr->data[sptr->top]=num;
    }
}
char pop(struct stack *sptr)
{
    int num;
    num=sptr->data[sptr->top];
    sptr->top--;
    return num;
}

void check(struct stack *sptr,char no[SIZE])
{
    int ch,stat=1;
    int i=0;
    char temp;
    while(no[i]!='\0')
    {
        ch=no[i];
        push(sptr,ch);
        i++;
    }
    i=0;
    while(no[i]!='\0')
    {
        temp=no[i];
        if(temp!=pop(sptr))
        {
            stat=0;
            break;
        }
        i++;
    }
    if(stat==1)
    {
        printf("%s is a palindrome string",no);
    }
    else
    {
        printf("%s is not a palindrome string",no);
    }
    
    
}
int main()
{
    struct stack * sptr;
    struct stack s;
    char pa[SIZE];
    sptr=&s;
    sptr->top=-1;
    scanf("%s",pa);
    check(sptr,pa);
    return 0;
}

OUTPUT

Congratulations, you passed the sample test case.

Click the Submit Code button to run your code against all the test cases.

Input (stdin)

GADAG
Your Output (stdout)

GADAG is a palindrome string
Expected Output

GADAG is a palindrome string

 

Please find some more codes of Loops, Condition Statements, 1D Arrays, 2D Arrays, Strings, Pointers, Data Structures, Files, Linked lists, Stacks, Queues, Binary Trees, MISC, Solved model question papers & Hacker Rank all solutions on the below page:

Top 100+ C Programming codes – KLE Technological University







Leave a Comment

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

Welcome to FactsPrime

Sorry, We have detected that you have activated Ad-Blocker. Please Consider supporting us by disabling your Ad Blocker, It helps us in maintaining this website. To View the content, Please disable adblocker and refresh the page.

Thank You !!!