C Program for sorting the list of names
Write a C program which will accept multiple strings fron the user and will sort them in ascending order.
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<conio.h>
void main()
{
char *string1[5],*temp;
int i,j,n;
clrscr();
printf("\nHow many names do you want to have?");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n Enter the name %d: ",i);
flushall();
gets(string1[i]);
}
for(i=0;i<n;i++)
{
for(j=0;j<n-1;j++)
{
if(strcmp(string1[j],string1[j+1])>0)
{
strcpy(temp,string1[j]);
strcpy(string1[j],string1[j+1]);
strcpy(string1[j+1],temp);
}
}
}
printf("\nSorted List : ");
flushall();
for(i=0;i<n;i++)
puts(string1[i]);
getch();
}
Related Articles:
- C Program to Implement Stack Operations using Singly Linked List
- Display Singly Linked List from First to Last
- Sorting Two Structures on the basis of any structure element and Display Information
- Counting number of Nodes in Singly Linked List
- Program to Concat Two Strings with using Library Function : Strcat
- C Program to Sort set of strings in alphabetical order using strcmp()
- Traversing through Singly Linked List (SLL)
- C Program to Concat Two Strings without Using Library Function
- C Program to Compare Two Strings Without Using Library Function [Strcmp]
- Circular Linked List : C Programming Data Structure
About the author: Pritesh View all posts by Pritesh
My name is Pritesh Taral. I am working in well known MNC. as Java Developer. I am part time blogger loves writing articles on C/C++.
I am active on facebook using community fan page .One can Visit me @ Facebook
Facebook Fan Page