how to write a c program to print list of fruits in
alpabetical order?
Answer Posted / 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 |
Post New Answer View All Answers
what is associativity explain what is the precidence for * and & , * and ++ how the folloing declaration work 1) *&p; 2) *p++;
can any one please explain, how can i access hard disk(physical address)? it is possible by the use of far,near or huge pointer? if yes then please explain......
How can I implement sets or arrays of bits?
what is ur strangth & weekness
write a program to find out prime number using sieve case?
What are compound statements?
In C language, the variables NAME, name, and Name are all the same. TRUE or FALSE?
Is int a keyword in c?
ATM machine and railway reservation class/object diagram
Sir,please help me out with the code of this question. Write an interactive C program that will encode or decode multiple lines of text. Store the encoded text within a data file, so that it can be retrieved and decoded at any time. The program should include the following features: (a) Enter text from the keyboard, encode the text and store the encoded text in a data file. (b) Retrieve the encoded text and display it in its encoded form. (c) Retrieve the encoded text, decode it and then display the decoded text. (d) End the computation. Test the program using several lines of text of your choice.
what is associativity explain what is the precidence for * and & , * and ++ how the folloing declaration work 1) *&p; 2) *p++;
What is the general form of a C program?
What is the difference between union and anonymous union?
List at least 10 sorting methods indicating their average case complexity, worst case complexity and best case complexity.
What are pointers? What are different types of pointers?