google.com, pub-4617457846989927, DIRECT, f08c47fec0942fa0 Learn to enjoy every minute of your life.Only I can change my life.: C program in stack to concatenate two strings using push and pop functions

Tuesday, April 7, 2015

C program in stack to concatenate two strings using push and pop functions

#include<stdio.h>
#include<conio.h>
#include<string.h>
#define max 30
void push(char ch);
char pop();
char stack[max];
int top=-1;
void main()
{
char a[20],b[20],k[40],ch;
int i,l1,l2;
clrscr();
printf("\n Enter First String:-\n");
scanf("%s",a);
printf("\n Enter Second String:-\n");
scanf("%s",b);
l1=strlen(a);
l2=strlen(b);
for(i=0;i<l2;i++)
{
 push(b[i]); //push second string into stack
}
push(' ');
for(i=0;i<l1;i++)
{
 push(a[i]); //push first string into stack
}
printf("\n After concatenation of two string:-\n");
i=0;
while(top!=-1)
{
 ch=pop();
 if(ch==' ')
{
k[i]='\0';
printf("%s",strrev(k));
i=0;
 }
else
 {
  k[i]=ch;
  i++;
 }
}
k[i]='\0';
printf("%s",strrev(k));
getch();
}
void push(char ch)
{
if(top==max-1)
{
 printf("\n Stack is full");
}
else
{
 top++;
 stack[top]=ch;
}
}

char pop()
{
char t;
if(top==-1)
{
 printf("\n Stack is Empty");
}
else
{
 t=stack[top];
 top--;
 return(t);
}
}

अच्छे विचार करे विचार

  पहचान की नुमाईश, जरा कम करें... जहाँ भी "मैं" लिखा है, उसे "हम" करें... हमारी "इच्छाओं" से ज़्यादा "सुन...