C++ gurus and learners, we program ourselves :P all work is CC licensed

Wednesday, December 11

sorting of character arrays based on first character only

18:50 By

A code for Sir Saeed

/**
 * code by http://CppQA.blogspot.com
 * sorting of character arrays based on first character only
 * C++
*/
#include <iostream>
#include <string.h>
using namespace std;

int main() {
 char countries[5][15] = {"Pakistan","India","Afghanistan","Iran","China"};
 for(int i=0;i<5;i++)
 {
  for(int j=0;j<5-1;j++)
  {
   if(countries[i][0] < countries[j][0])
   {//swaping
    char temp[15];
    strcpy(temp,countries[i]);
    strcpy(countries[i],countries[j]);
    strcpy(countries[j],temp);
   }
  }
 }
 
 //print
 for(int i=0;i<5;i++)cout<<countries[i]<<endl;
 return 0;
}

6 comments :