wtie a program which take as tring and arrange in alphabetic order

#include<stdio.h>
#include<conio.h>
#include<iostream.h>
void main (void)
{
 clrscr();
 char b[7]={98,120,97,113,122,121,101},temp=0;
 while (!(b[0]<b[1]&&b[1]<b[2]&&b[2]<b[3]&&b[3]<b[4]&&b[4]<b[5]&&b[5]<b[6]&&b[6]<b[7]))
 {
  if (b[0]>b[1])
  {
   temp=b[0];
   b[0]=b[1];
   b[1]=temp;
  }
  if (b[1]>b[2])
  {
   temp=b[1];
   b[1]=b[2];
   b[2]=temp;
  }
  if (b[2]>b[3])
  {
   temp=b[2];
   b[2]=b[3];
   b[3]=temp;
  }
  if (b[3]>b[4])
  {
   temp=b[3];
   b[3]=b[4];
   b[4]=temp;
  }
  if (b[4]>b[5])
  {
   temp=b[4];
   b[4]=b[5];
   b[5]=temp;
  }
  if (b[5]>b[6])
  {
   temp=b[5];
   b[5]=b[6];
   b[6]=temp;
  }
  if (b[6]>b[7])
  {
   temp=b[6];
   b[6]=b[7];
   b[7]=temp;
  }
 }
 for (int c=0;c<=0;c++)
 {
  cout<<b;
 }
 getch();
}

Recommended Answers

All 6 Replies

first find the length of the string(str) say len. then

for(int i=0;i<len;i++)
{
for(int j=i+1;j<len;j++)
{
if(str[i]>str[j])
temp=str[i];
str[i]=str[j];
str[j]=temp;
}
}

Yes i'm using turboc 3.0

>>char b[7]={98,120,97,113,122,121,101},

what is that? If you want to create a char array, do it the normal way so that you don't have to look up those numbers in an ascii table. And you don't have to specify the length unless you want the array to be larger than the initialization string. char b[] = "Hello"; There are a number of C sort algorithms that will sort the array -- the bubble sort is the easiest to code, but also the slowest. It can be coded in about 10 lines of code. Its ok on very small arrays such as what you are using. If you don't know how to code bubble sort just google for it and you will find lots of examples.

unless your programming is done, here's a code that might possibly help you out in turboc3......
//program to print string in an alphabetical order

#include<iostream.h>
#include<string.h>
#include<conio.h>
void main()
{
char a[20];
int i,j,k;
clrscr();
cout<<"enter the string";
cout<<"\n\tThe string in alphabetical order is";
for(i=0;a[i]!='\0';i++) //sorting in alphabetical order
{
j=i;
for(k=i+1;a[k]!='\0';k++)
{
if(a[k]<a[j])
j=k;
}
k=a[i];
a[i]=a[j];
a[j]=char(k);
cout<<a[i];
}
getch();
}

execute it and see if it works............probably will as i've already checked it out myself..........
kanika;)

unless your programming is done, here's a code that might possibly help you out in turboc3......
//program to print string in an alphabetical order

#include<iostream.h>
#include<string.h>
#include<conio.h>
void main()
{
char a[20];
int i,j,k;
clrscr();
cout<<"enter the string";
cout<<"\n\tThe string in alphabetical order is";
for(i=0;a[i]!='\0';i++) //sorting in alphabetical order
{
j=i;
for(k=i+1;a[k]!='\0';k++)
{
if(a[k]<a[j])
j=k;
}
k=a[i];
a[i]=a[j];
a[j]=char(k);
cout<<a[i];
}
getch();
}

execute it and see if it works............probably will as i've already checked it out myself..........
kanika;)

thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.