C program to sort the string in alphabetical order.
In this tutorial we will learn how to write a c program that print the sort order of names.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int i,j,n;
char str[100][100],s[100];
printf("\n ENTER NUMBER OF NAMES OR STRING:\n ");
scanf("%d",&n);
printf("\n ENTER NAMES OR STRING:\n ");
for(i=0;i<n;i++)
{
scanf("%s",str[i]);
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(strcmp(str[i],str[j])>0)
{
strcpy(s,str[i]);
strcpy(str[i],str[j]);
strcpy(str[j],s);
}
}
}
printf("\n THE SORTED ORDER OF NAMES OR ALPHABATES ARE:\n ");
for(i=0;i<n;i++)
{
printf("%s\n",str[i]);
}
getch();
}
In the first we will enter the numbers or range of names then we will enter the names.
In this tutorial we will learn how to write a c program that print the sort order of names.
C program code
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int i,j,n;
char str[100][100],s[100];
printf("\n ENTER NUMBER OF NAMES OR STRING:\n ");
scanf("%d",&n);
printf("\n ENTER NAMES OR STRING:\n ");
for(i=0;i<n;i++)
{
scanf("%s",str[i]);
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(strcmp(str[i],str[j])>0)
{
strcpy(s,str[i]);
strcpy(str[i],str[j]);
strcpy(str[j],s);
}
}
}
printf("\n THE SORTED ORDER OF NAMES OR ALPHABATES ARE:\n ");
for(i=0;i<n;i++)
{
printf("%s\n",str[i]);
}
getch();
}
OUTPUT:
In the first we will enter the numbers or range of names then we will enter the names.
Please share this post and blog link with your friends.For more programs use this blog.
If you have any problem, please comment in comment box, subscribe this blog for notifications of new post on your email and follow this blog.If you have any method of this program or want to give any suggestion send email on hc78326@gmail.com
Created by-- HARSH CHAUHAN
No comments:
Post a Comment