how to write a c program to print list of fruits in
alpabetical order?
Answer / Ajay Shankar Yadava
To create a C program that prints a list of fruits in alphabetical order, you can use an array or linked list and sort the elements using a comparison function such as 'qsort' from the standard library. Here's a simple example:
```c
#include <stdio.h>
#include <string.h>
struct Fruit {
char name[20];
};
int compare(const void *a, const void *b) {
return strcmp(((struct Fruit *)a)->name, ((struct Fruit *)b)->name);
}
void printFruits(struct Fruit fruits[], int size) {
qsort(fruits, size, sizeof(fruits[0]), compare);
for (int i = 0; i < size; ++i) {
printf("%sn", fruits[i].name);
}
}
int main() {
struct Fruit fruits[] = {"apple", "banana", "grape", "kiwi", "orange"};
printFruits(fruits, sizeof(fruits) / sizeof(fruits[0]));
return 0;
}
| Is This Answer Correct ? | 0 Yes | 0 No |
Explain what is the difference between a free-standing and a hosted environment?
Write a program that his output * *** *****
Famous puzzles which are generally asked by companies during interviews ?
Name the language in which the compiler of "c" in written?
What is function prototype in c language?
how to implement stack operation using singly linked list
What are high level languages like C and FORTRAN also known as?
What is a keyword?
What is c preprocessor mean?
Write a C program to fill a rectangle using window scrolling
how to make c program without a libary? e.g.#include<stdio.h> libary is not in c progaram.
What is actual argument?