Home » Code Snippets » C/C++ Code Snippets

C program to remove a patient name while discharging from hospital

This program is asked by Navya through comment on 24/11/2016.

Question

Consider a hospital with list of patients in a ward. The patient "GREEN" is discharged. What are the changes you have to make to maintain the list?
There are patients named "RED","GREEN","BLUE","BLACK" and "YELLOW" in the list....i need to know the changes which can be made in the program if the patient "GREEN" gets discharged.

Answer

This is a simple program, which has 5 patients in a string array, names are displaying in the program and program will ask to the user to enter patient name that is going to be discharged, after that program will print the list of available patients.

Remove an item from the list (string array) in C

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

int main(){
	char patients[5][30]={"RED","GREEN","BLUE","BLACK","YELLOW"};
	char name[30];
	int n=5,i,j;
	
	
	printf("All patients are:\n");
	for(i=0;i<n;i++){
		printf("%d : %s\n",i,patients[i]);
	}
	
	printf("Enter name of patients to remove: ");
	scanf("%s",name);
	
	//compare and remove from the list 
	for(i=0;i<n;i++){
		if(strcmp(patients[i],name)==0){
			//if name found shift other names
			for(j=i;j<(n-1);j++){
				strcpy(patients[j],patients[j+1]);
				
			}
	        n--; //decrease the value of n
			break;
		}
	}
	
	printf("Available patients are:\n");
	for(i=0;i<n;i++){
		printf("%d : %s\n",i,patients[i]);
	}
	
	return 0;
}

Output

All patients are: 
0 : RED 
1 : GREEN 
2 : BLUE
3 : BLACK 
4 : YELLOW
Enter name of patients to remove: GREEN 
Available patients are: 
0 : RED 
1 : BLUE
2 : BLACK 
3 : YELLOW

COMMENTS




Copyright © 2024 www.includehelp.com. All rights reserved.