C Program : Let us understand the program shared on 13/07/2023

in c-programming •  last year 

Welcome back,
In today's blog we will see the explanation for our previously shared program. You can find that in my previous blog so you can go and visit it as well.

Let us look at the program,

Screenshot 2023-07-13 210114.png

Screenshot 2023-07-13 210127.png

Screenshot 2023-07-13 210138.png

Screenshot 2023-07-13 210151.png

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

#define MAX_ANIMALS 100
#define MAX_NAME_LENGTH 100
#define MAX_OWNER_LENGTH 100

typedef struct {
char name[MAX_NAME_LENGTH];
char owner[MAX_OWNER_LENGTH];
} Animal;

int main() {
Animal animals[MAX_ANIMALS];
int numAnimals = 0;

// Input animals and their owners
printf("Enter the details of the animals:\n");
printf("Enter 'exit' to stop.\n");

while (1) {
char name[MAX_NAME_LENGTH];
char owner[MAX_OWNER_LENGTH];

printf("Enter animal name: ");
fgets(name, MAX_NAME_LENGTH, stdin);
name[strcspn(name, "\n")] = '\0'; // Remove the trailing newline

// Check if the user wants to exit
if (strcmp(name, "exit") == 0) {
break;
}

printf("Enter owner name: ");
fgets(owner, MAX_OWNER_LENGTH, stdin);
owner[strcspn(owner, "\n")] = '\0'; // Remove the trailing newline

// Store the animal and owner details
if (numAnimals < MAX_ANIMALS) {
strcpy(animals[numAnimals].name, name);
strcpy(animals[numAnimals].owner, owner);
numAnimals++;
printf("Animal details added.\n");
} else {
printf("Maximum number of animals reached.\n");
break;
}
}

// Search for an animal
printf("\nEnter the name of an animal to search: ");
char searchName[MAX_NAME_LENGTH];
fgets(searchName, MAX_NAME_LENGTH, stdin);
searchName[strcspn(searchName, "\n")] = '\0'; // Remove the trailing newline

// Iterate over the animals to find a match
int found = 0;
for (int i = 0; i < numAnimals; i++) {
if (strcmp(searchName, animals[i].name) == 0) {
printf("Owner of %s: %s\n", animals[i].name, animals[i].owner);
found = 1;
break;
}
}

if (!found) {
printf("Animal not found.\n");
}

return 0;
}

Let us now understand the program written above.

We start by including the necessary header files: stdio.h for standard input/output operations and string.h for string manipulation functions.

We define some constants:

MAX_ANIMALS specifies the maximum number of animals that can be stored.
MAX_NAME_LENGTH specifies the maximum length of an animal's name.
MAX_OWNER_LENGTH specifies the maximum length of an owner's name.
We define a structure called Animal which has two members: name to store the animal's name and owner to store the owner's name.

In the main() function, we declare an array of Animal structures called animals to store the animals and their owners. We also declare a variable numAnimals to keep track of the number of animals stored.

The program prompts the user to enter the details of the animals. It repeatedly asks for the animal's name and owner's name until the user enters "exit" as the name. Each animal's name and owner's name are stored in the animals array.

After inputting the animal details, the program prompts the user to enter the name of an animal to search for.

The program then iterates over the animals array and checks if the entered name matches any of the stored animal names using the strcmp() function. If a match is found, it displays the corresponding owner's name.

If no match is found, the program displays a message indicating that the animal was not found.

Finally, the program terminates, and 0 is returned to indicate successful execution.

If you also want to learn basics and start your C programming journey then find a teacher today for free on Youtube.

I learnt alot from a teacher who teaches c in hindi and if you are finding someone who teaches in hindi then this person will definitely help you.

Source

I am happy to call myself a trader and small programmer at the same time now.

Happy trading and keep learning what you love.

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE BLURT!