About Me

My photo
Raipur, Chhattisgarh, India
Hi , I am Amit Thakur. I have worked as a QA Engineer for two years and as a Java Developer for one year in NIHILENT TECHNOLOGIES PVT. LTD., Pune.Currently I am working as DEAN (Research & Development) in Bhilai Institute of Technology, Raipur.

Tuesday, September 3, 2013

C Program to Sort set of strings in alphabetical order using strcmp()

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

No comments: