C++ Programming Blog

 
 
 

Image

 # include <iostream.h>
 # include <fstream.h>
 # include <string.h>
 # include <stdlib.h>
 # include <conio.h>

 int main( )
    {
       clrscr( );

       fstream File(\"CP-05.txt\",ios::in|ios::nocreate);

       if(!File)
      {
         cout<<\"\\n Unable to open the input file.\"<<endl;
         cout<<\"\\n Press any key to exit.\";

         getch( );
         exit(EXIT_FAILURE);
      }

       char Number_of_strings[85]={NULL};
       char Strings[50][50]={NULL};
       char Temp[50]={NULL};

       int number_of_strings=0;

       File.getline(Number_of_strings,80);

       number_of_strings=atoi(Number_of_strings);

       int i=0;
       int j=0;

       for(i=0;i<number_of_strings;i++)
      File.getline(Strings[i],26);

       for(i=0;i<(number_of_strings-1);i++)
      {
         for(j=0;j<(number_of_strings-1);j++)
        {
           if(strcmpi(Strings[j],Strings[j+1])>0)
              {
             strset(Temp,NULL);
             strcpy(Temp,Strings[j]);
             strset(Strings[j],NULL);
             strcpy(Strings[j],Strings[j+1]);
             strset(Strings[j+1],NULL);
             strcpy(Strings[j+1],Temp);
              }
        }
      }

       cout<<\" *****  Ascending Order  *****\"<<endl;

       for(i=0;i<number_of_strings;i++)
      cout<<Strings[i]<<endl;

       for(i=0;i<(number_of_strings-1);i++)
      {
         for(j=0;j<(number_of_strings-1);j++)
        {
           if(strcmpi(Strings[j],Strings[j+1])<0)
              {
             strset(Temp,NULL);
             strcpy(Temp,Strings[j]);
             strset(Strings[j],NULL);
             strcpy(Strings[j],Strings[j+1]);
             strset(Strings[j+1],NULL);
             strcpy(Strings[j+1],Temp);
              }
        }
      }

       cout<<endl<<\" *****  Descending Order  *****\"<<endl;

       for(i=0;i<number_of_strings;i++)
      cout<<Strings[i]<<endl;

       getch( );
       return 0;
    }