Wednesday 12 April 2017

Remove spaces from string Using Queues: Cprogram

Write a program that compresses a string by deleting all space characters in the string. One way to do so is to use a queue of characters. Insert non space  characters from the string into the queue. When you reach the end of the string , dequeue the characters from the queue and place them back into the string.  

#include <stdio.h>
#define MAX 50
char q[MAX],f=-1,r=-1;

void enq(char val)
{
                if(r == MAX-1)
                                printf("queue is full and hence cannot insert");
                else if(f == -1 && r == -1)
                                f=r=0;
                else
                                r=r+1;
                q[r]=val;
}
char deq()
{
                char val;
                if(f == -1)
                                printf("queue is empty and hence cannot delete");
                else
                {
                                val = q[f];
                                if(f == r)
                                                f=r=-1;
                                else
                                                f=f+1;
                }
                return val;
}
int main()
{
                int i;
                char s[MAX];
                gets(s);
                for(i=0;s[i]!='\0';i++)
                {
                                if(s[i] != ' ')
                                                enq(s[i]);
                }
                for(i= f;i<=r;i++)
                                s[i]=deq();
                s[i]='\0';
                puts(s);

}

Output:


No comments:

Post a Comment