Problem Statement : Write a C Program that will accept set of 5 strings and sort them using strcmp library function and print the result on the screen.
Program to sort set of strings in alphabetical order
#include<stdio.h>
#include<string.h>
void main()
{
char s[5][20],t[20];
int i,j;
clrscr();
printf("Enter any five strings : \n");
for(i=0;i<5;i++)
scanf("%s",s[i]);
for(i=1;i<5;i++)
{
for(j=1;j<5;j++)
{
if(strcmp(s[j-1],s[j])>0)
{
strcpy(t,s[j-1]);
strcpy(s[j-1],s[j]);
strcpy(s[j],t);
}
}
}
printf("Strings in order are : ");
for(i=0;i<5;i++)
printf("\n%s",s[i]);
getch();
}
Output :
Enter any five strings :
pri
pra
pru
pry
prn
Strings in order are :
pra
pri
prn
pru
pry
Related Articles:
- C Program to Compare Two Strings Without Using Library Function [Strcmp]
- Program to Concat Two Strings with using Library Function : Strcat
- C Program to Concat Two Strings without Using Library Function
- strcmp function >> string.h >> Compare two strings
- C Program to Implement Bubble Sort in C Programming
- C Program to Solve Second Order Quadratic Equation
- Program to Implement Insertion Sort in C Programming
- Program to copy one string into other with using library function [ strcpy ]
- Character Set in C Programming
- Printf MCQ 9 : Multiple Strings in Printf
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