Close
Close

Modified Code

   pawansaggu

#include <iostream>
#include <string>
using namespace std;

int main()
{char name[5][10]={"Pawan",
                  "Himanshu",
                  "Aayush",
                  "Aayushi",
                  "Lakhay"};
                  char t;          //Here i defined a new character t.
                  
   for(int i=0;i<4;i++)          //first loop which specifies every name.
   {
           for(int j=i+1;j<5;)       //second loop for comparing first name to every other name.
           {
                       for(int x=0;x<10;)         //I defined another integet x which runs from first character of every name to last character.
                       {
                               if(name[i][x]>name[j][x])      //This condition checks if the first character of names.
                               {
                                     for(int k=0;k<10;k++)      //This loop SWAPS every character one by one.
                                                        
                                                  {t=name[i][k];
                                                  name[i][k]=name[j][k];
                                                 name[j][k]=t;}
                                                  
                                                 j++;}                      //Here j++ moves the loop to the next name.
                                                         
                               else if (name[i][x]<name[j][x]){j++;}//if first character is not greater than j++ moves the loop forward to the next name;
                            
                             else{x++;}//if first characters are equal then x++ moves the loop to the next character for comparison.
                                                                   
                                                                   
                                                                   
                                                                   }
                                                                   }
                                                                   }
                                                                                           
                                                                                                         
//For writing the sorted string.                
for(i=0;i<5;i++){for(int y=0;y<10;y++){cout<<name[i][y];}
cout<<endl;}


  system("pause");

  return 0;
}
Sorry,i know this code is messy and difficult to understand.


Answers

Ask Yours
Post Yours
Write your answer