Slip 21_1: Write a program that reverses a string of characters. The function should use a stack library (cststack.h). Use a static implementation of the stack.

 Solution

Header File : cststack.h

#include<stdio.h>

char s[20];

int top;

void init()

{

 top==-1;

}

int isempty()

{

 if(top==-1)

 return 1;

 else

 return 0;

}

int isfull()

{

 if(top==19)

 return 1;

 else

 return 0;

}

void push(char ch)

{

 if(isfull()==1)

 printf("Stack is full");

 else

 {

 top++;

 s[top]=ch;

 }

}

char pop()

{

 char ch;

 if(isempty()==1)

 printf("Stack is empty");

 else

 {

 ch=s[top];

 top--;

 return ch;

 }

}

Program File :

#include<stdio.h>

#include"stack.h"

int main()

{

 init();

 char str[20];

 int i;

 printf("Enter String: ");

 scanf("%s",&str);

 for(i=0;str[i]!='\0';i++)

 {

 push(str[i]);

 }

 printf("Reversed string: ");

 while(!isempty())

 {

 printf("%c",pop());

 }

}



Comments

Popular posts from this blog

Slip 22_2: Read the data from file 'cities.txt' containing names of cities and their STD codes. Accept a name of the city from user and use sentinel linear search algorithm to check whether the name is present in the file and output the STD code, otherwise output “city not in the list”. Solution :

Slip10_2, 30_1 : Read the data from the file “employee.txt” and sort on names in alphabetical order (use strcmp) using bubble sort or selection sort

Slip 10_1 ,22_1: Implement a linear queue library (st_queue.h) of integers using a static implementation of the queue and implementing the init(Q), add(Q) and peek(Q) operations. Write a program that includes queue library and calls different queue operations