Friday 14 April 2017

Group all occurrences of characters according to first appearance: C Program

Given a string of lowercase characters, the task is to print the string in a manner such that a character comes first in string displays first with all its occurrences in string.
Examples:
Input :  str = "occurrence"
output : occcurreen 

Input  : str = "cdab"
Output : cdab



C Program

#include <stdio.h>
#include <string.h>
main()
{
char st[100],temp,s[100];
int i,k=0,j,flag=0;
gets(st);
               
for(i=0;st[i]!='\0';i++)
{
                for(j=i;st[j]!='\0';j++)
                {
                                if(k<strlen(st))
                                {
                                                if(st[i] == st[j])
                                                                s[k++]=st[j];      
                                }
                                else
                                {
                                                flag=1;
                                                break;
                                }                                             
                }
                if(flag ==1)
                                break;
}
s[k]='\0';
puts(s);
}

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:


Copying elements of one queue to another queue : C program

#include <stdio.h>
#define MAX 5
int q1[MAX],q2[MAX],f1=-1,r1=-1,f2=-1,r2=-1;
void enq(int q[],int *f,int *r,int 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;
}
void display(int q[],int *f, int *r)
{
                int i;
                if(*f == -1)
                                printf("queue is empty ");
                else
                {
                                for(i=*f;i<=*r;i++)
                                                printf("%d\t",q[i]);
                }             
}
main()
{
                int i,val;
                for(i=0;i<MAX;i++)
                {
                                printf("\nenter a value\n");
                                scanf("%d",&val);
                                enq(q1,&f1,&r1,val);
                }
                printf("\nelements in queue 1 are\n");
                display(q1,&f1,&r1);
               
                for(i=f1;i<=r1;i++)
                                enq(q2,&f2,&r2,q1[i]);
                printf("\nelements in queue 2 are\n");
                display(q2,&f2,&r2);

}

Output: