Saturday, January 8, 2011

C Program to Sorting a characters in the String

Coding:


#include<stdio.h> 
#include<string.h> 
#include<conio.h> 
void main()
{
char a[200],temp;
int n=0,j,i;
clrscr();
textcolor(23);
cprintf("\nEnter the string: ");
gets(a);
while(a[n]!='\0')
{
n++;
}
for(i=0;i<n;i++)
{
for(j=0;j<n-i-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
textcolor(23);
cprintf("\nThe string in alphabetical order:");
for(i=0;i<n;i++)
{
textcolor(10);
cprintf("%c",a[i]);
}
getch();
}

Output:



No comments: