Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

A stack of words in C (Sentence Palindrome)

cstacksstrings linked-list palindrome

This topic has been archived. This means that you cannot reply to this topic.
No replies to this topic

#1 albim1998

albim1998

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 24 February 2017 - 05:37 AM

I was given this problem and I needed to create a code for it. So we have a string which is inputed by the user and then the code needs to check if the sentence is a palindrome or not ( the symmetric words to the middle of the sentence should be the same. But we should implement this using a stacks. I am familiar with functions pop() and push() (even thought I have not used them below). What I have thought until now is that I take the strings and create a stack of words out of this strings and use that stack to check if the sentence is a palindrome. I am stuck now and I can't really think of anything else. Help would be much appreciated.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct stack
{
    char s[30];
    struct stack *next;
};

typedef struct stack STACK;

struct top
{
    int num;
    struct stack *top;
};

typedef struct top TOP;

void create_stack(TOP *s, char str[1000])
{
    char temp1[30];
    int i=0, j=0;

    STACK *temp;
    temp=(STACK*)malloc(1*sizeof(STACK));

    while(1)
    {
        if(str[i]!=' ' && str[i]!='\0')
        {
            temp1[j]=str[i];
            j++;
        }
        else
        {
            temp1[j]='\0';
            strcpy(temp->s,temp1);
            printf("%s\n", temp->s);

            if(s->top==NULL)
            {
                s->top=temp;
                s->num=1;
            }
            else
            {
                temp->next=s->top;
                s->top=temp;
                s->num++;
            }
            j=0;
        }
        if(str[i]=='\0')
        {
            break;
        }
        i++;
    }
}

void move_cursor(STACK *cursor, int pos)
{
    while (pos!=0)
    {
        cursor=cursor->next;
        pos--;
    }
}

void compare(TOP *s)
{
    STACK *cursor1, *cursor2;
    cursor1=s->top;
    cursor2=s->top;
    int cursor_move1, cursor_move2, i=0, check=1;

    if(s->num%2==0)
    {
        cursor_move1=s->num/2;
        cursor_move2=(s->num/2)+1;

        while (i!=cursor_move1)
        {
            cursor1=s->top;
            cursor2=s->top;
            move_cursor(cursor1, i);
            move_cursor(cursor2, cursor_move2);

            if(strcmp(cursor1->s,cursor2->s)!=0)
            {
                check=0;
                break;
            }
            else
            {
                i++;
                cursor_move2++;
            }
        }
    }

    if(check==0)
        printf("%d Neg", check);
    else
        printf("1Pos");
}

void display(TOP *top)
{
    STACK *cursor;
    cursor=top->top;

    while(cursor->next==NULL)
    {
        printf("%s pos\n ", cursor->s);

        cursor=cursor->next;
    }
}

int main()
{
    char input[1000];
    TOP top;
    top.num=0;
    top.top=NULL;

    fgets(input, 100, stdin);

    input[strlen(input)-1]='\0';

    create_stack(&top, input);

    printf("%d \n ", top.num);

    display(&top);
    printf("---------------------------------------------------------\n");
    compare(&top);


    return 0;
}





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download