Palindrome using C Programming

Embed Size (px)

Citation preview

  • 7/28/2019 Palindrome using C Programming

    1/19

    /*This program will recognise a string input by user is in the

    language L,

    where L = {w$w': w is a possibly empty string of characters

    other than $, w' = reverse(w)}*/

    #include /* Define standard input output routine */

    /* Declaration of Stack data structure */

    #define SIZE 10 /* Size of Stack */

    typedef struct{

    int items[SIZE];

    int top;

    }

    STACK;

    /* Declaration of function prototypes */

    void push();

    int pop();

    void display();

    int is_overflow();

    int is_empty();

    int main(){

    STACK stck;

    char str[100];

    int a, flag;

    stck.top = -1;

  • 7/28/2019 Palindrome using C Programming

    2/19

    printf("This program will check whether the string is in

    language\n");

    printf("L = {w$w': w is a possibly empty string of

    characters \n");

    printf("other than $, w' = reverse(w)}\n");

    printf("========================================================

    =\n");

    printf("Enter a string: "); /* Prompt user to input */

    gets(str);

    for(a=0;str[a]!='\0'; a++)

    push(&stck, str[a]);

    flag = 1;

    for(a=0;str[a]!='$';a++){ /* Character $

    will act as mirror between */

    if(str[a] != pop(&stck)){ /* left and right

    char of the string */

    flag = 0;

    break;

    }

    }

    if(flag == 1){

    printf("\nThe above string is in the

    language\n");

    }

    else{

  • 7/28/2019 Palindrome using C Programming

    3/19

    printf("\nThe above string is not in the

    language\n");

    }

    getchar();

    return 0;

    }//main

    //FUNCTIONS

    void push(STACK *p, int element){ /* Function for PUSH

    operation */

    (p->top)++;

    p->items[p->top] = element;

    }

    int pop(STACK *p){

    return (p->items[(p->top)--]); /* Function for POP

    operation */

    }

    /*Function to check stack overflow condition */

    int is_overflow(int top){

    if(top == SIZE - 1)

    return(1);

    else

    return(0);

    }

  • 7/28/2019 Palindrome using C Programming

    4/19

    /* Function to check stack empty condition */

    int is_empty(int top){

    if(top == -1)

    return(1);

    else

    return(0);

    }

  • 7/28/2019 Palindrome using C Programming

    5/19

    /* Write a C program to implement stack. Stack is a LIFO data

    strcuture *

    * LIFO - Last in First Out

    *

    * Perform PUSH(insert operation), POP(Delete operation) and

    Display stack */

    #include

    #include

    #define MAXSIZE 5

    struct stack /* Structure definition for stack */

    {

    int stk[MAXSIZE];

    int top;

    };

    typedef struct stack STACK;

    STACK s;

    /* Function declaration/Prototype*/

    void push (void);

    int pop(void);

    void display (void);

    int main ()

  • 7/28/2019 Palindrome using C Programming

    6/19

    {

    int choice;

    int option = 1;

    s.top = -1;

    printf ("STACK OPERATION\n");

    while (option)

    {

    printf ("-----------------------------------

    -------\n");

    printf (" 1 --> PUSH

    \n");

    printf (" 2 --> POP

    \n");

    printf (" 3 --> DISPLAY

    \n");

    printf (" 4 --> EXIT \n");

    printf ("-----------------------------------

    -------\n");

    printf ("Enter your choice\n");

    scanf ("%d", &choice);

    switch (choice)

    {

    case 1: push();

  • 7/28/2019 Palindrome using C Programming

    7/19

    break;

    case 2: pop();

    break;

    case 3: display();

    break;

    case 4: return;

    }

    fflush (stdin);

    printf ("Do you want to continue(Type 0 or

    1)?\n");

    scanf ("%d", &option);

    }

    getch();

    return 0;

    }

    /*Function to add an element to the stack*/

    void push ()

    {

    int num;

    if (s.top == (MAXSIZE - 1))

    {

    printf ("Stack is Full\n");

    return;

    }

  • 7/28/2019 Palindrome using C Programming

    8/19

    else

    {

    printf ("Enter the element to be pushed\n");

    scanf ("%d", &num);

    s.top = s.top + 1;

    s.stk[s.top] = num;

    }

    return;

    }

    /*Function to delete an element from the stack*/

    int pop ()

    {

    int num;

    if (s.top == - 1)

    {

    printf ("Stack is Empty\n");

    return (s.top);

    }

    else

    {

    num = s.stk[s.top];

    printf ("poped element is = %d\n",

    s.stk[s.top]);

    s.top = s.top - 1;

  • 7/28/2019 Palindrome using C Programming

    9/19

    }

    return(num);

    }

    /*Function to display the status of the stack*/

    void display ()

    {

    int i;

    if (s.top == -1)

    {

    printf ("Stack is empty\n");

    return;

    }

    else

    {

    printf ("\nThe status of the stack is\n");

    for (i = s.top; i >= 0; i--)

    {

    printf ("%d\n", s.stk[i]);

    }

    }

    printf ("\n");

    }

  • 7/28/2019 Palindrome using C Programming

    10/19

  • 7/28/2019 Palindrome using C Programming

    11/19

    4 --> EXIT------------------------------------------Enter your choice3The status of the stack is

    784523Do you want to continue(Type 0 or 1)?1------------------------------------------

    1 --> PUSH2 --> POP

    3 --> DISPLAY4 --> EXIT

    ------------------------------------------Enter your choice2poped element is = 78Do you want to continue(Type 0 or 1)?1------------------------------------------

    1 --> PUSH2 --> POP

    3 --> DISPLAY

    4 --> EXIT------------------------------------------Enter your choice3The status of the stack is4523Do you want to continue(Type 0 or 1)?0

  • 7/28/2019 Palindrome using C Programming

    12/19

    #include

    #include

    #include

    #define SIZE 10

    typedef struct

    {

    int items[SIZE];

    int top;

    }STACK;

    void push();

    int pop();

    void display();

    int isoverflow();

    int isempty();

    int main()

    {

    STACK s;

    char str[100];

    int i, flag;

  • 7/28/2019 Palindrome using C Programming

    13/19

    s.top = -1;

    printf("\nEnter a string: ");

    gets(str);

    for(i=0;str[i]!='\0'; i++)

    push(&s, str[i]);

    flag = 1;

    for(i=0;str[i]!='\0';i++)

    {

    if(str[i] != pop(&s))

    {

    flag = 0;

    break;

    }

    }

    if(flag == 1)

    printf("\nEntered string is palindrome\n");

    else

    printf("\nEntered string is not a

    palindrome\n");

    getch();

    return 0;

  • 7/28/2019 Palindrome using C Programming

    14/19

    }

    void push(STACK *p, int element)

    {

    if(isoverflow(p->top))

    {

    printf("\nStack is overflow");

    }

    else

    {

    (p->top)++;

    p->items[p->top] = element;

    }

    }

    int pop(STACK *p)

    {

    if(isempty(p->top))

    {

    printf("\nStack is underflow");

    }

    else

    {

    return (p->items[(p->top)--]);

    }

  • 7/28/2019 Palindrome using C Programming

    15/19

    }

    void display(STACK s)

    {

    int i;

    if(isempty(s.top))

    {

    printf("\nStack is empty");

    }

    else

    {

    for(i=s.top; i>=0; i--)

    {

    printf("\n%d", s.items[i]);

    }

    }

    }

    //Function to check stack overflow condition

    int isoverflow(int top)

    {

    if(top == SIZE - 1)

    return (1);

    else

    return (0);

  • 7/28/2019 Palindrome using C Programming

    16/19

    }

    //Function to check stack empty condition

    int isempty(int top)

    {

    if(top == -1)

    return (1);

    else

    return (0);

    }

  • 7/28/2019 Palindrome using C Programming

    17/19

    // This program determine if a word is a palindrome or not using

    stack and pointer.

    #include

    #include

    #include

    #include

    #define SIZE 50

    //function declarations

    void push(char i);

    char pop(void);

    //pointers declarations and stack declaration

    char stack[SIZE];

    char *top = stack;

    char *bottom = stack;

    int main(){

    //declare a char array of size

    char string[SIZE];

    printf("Enter string: ");

    scanf("%s", &string);

  • 7/28/2019 Palindrome using C Programming

    18/19

    //printf("Your string is: %s \n", &string );

    int i;

    //push characters onto stack

    for (i = 0; i < strlen(string); i++)

    {

    if(isalnum(string[i]) && !isspace(string[i])) //if a

    character in the string s is a digit or alphabet character and

    not a white space

    {

    char c = tolower(string[i]);

    push(c);//push onto stack

    }

    }

    /* visualize the stack

    printf("Your stack currently has: ");

    for(i=0; i < strlen(string); i++){

    char temp=stack[i];

    printf("%c", temp);

    }

    printf("\n");*/

    // pop the stack

    for(i=0; i < strlen(string); i++){

    char currItem = pop();

  • 7/28/2019 Palindrome using C Programming

    19/19

    if(currItem != *bottom){

    printf("NOT a palindrome.\n");

    return 0;

    }

    else{

    bottom++;

    }

    }

    printf("This is a palindrome.\n");

    getch();

    return 0;

    }

    // push function

    void push(char i){

    *top = i;

    top++;

    }

    // pop function

    char pop(void){

    top--;

    return *top;

    }