Tutorials, Free Online Tutorials,It Challengers provides tutorials and interview questions of all technology like java tutorial, android, java frameworks, javascript, core java, sql, php, c language etc. for beginners and professionals.

Breaking

Data Structure - Stack Program for Reversing each word of a string

------------------------------------------ " Data Structure "---------------------------------------

STACK
#include<stdio.h> 
#include<conio.h> 
#include<string.h>

#define max 100
  char stack[max];
  int top=-1,bottom=-1;
  void push(char);
  char pop();
  int isempty(); 
  int isfull(); 

int isempty()    //IsEmpty Function

  return(top==bottom); 
}
 int isfull()       //IsFull Function
 { 
  return(top==max-1); 


void push(char ch)     //push Function
{
if(!isfull()) 
stack[++top]=ch; 
}
 char pop()               //Pop Function
 {
     if(!isempty())
return(stack[top--]); 


   //Main Function

void main()
 {
         char str[80];
int i,k; 
         clrscr(); 
         printf("\nEnter the Line Of Text : \n"); 
         gets(str); 
       for(i=0,k=0;i<=strlen(str);i++) 

if(str[i]==' '||str[i]=='\0') 
 { 
     while(!isempty()) 
  { 
    str[k++]=pop(); 
  } 
  str[k++]=str[i]; 
   }
 else 
    push(str[i]);
  }//end of for() 
  
  printf("\nReverse Of Each Word Of Line :\n");
      puts(str); 
getch(); 



No comments:

Post a Comment