Any help would be greatly appreciated. I am trying to sort 12 strings alphabetically which are contained in an array using bubble sort with the following code:
#include <stdio.h>
#include <string.h>
int main()
{
//initialising variables and the array
char *DT [12] = { "James Smith DT01 DT265A", "John Murphy DT02 DT265A", "Robert Lam DT03 DT265A", "Michael Martin DT04 DT265A", "William Brown DT05 DT265A", "David Roy DT06 DT265A", "Richard Tremblay DT07 DT265A", "Joseph Lee DT08 DT265A", "Thomas Gagnon DT09 DT265A", "Charles Wilson DT10 DT265A", "Chris Philips DT11 DT265A", "Henry Hill DT12 DT265A" } ;
char temp[100];
int n = sizeof(DT)/sizeof(DT[0]);
//Implementing Algorithm using bubble sort to sort string array
//
for (int j = 0 ; j < n - 1 ; j++ )
{
for (int i = j + 1 ; i < n ; i++ )
{
if( strcmp(DT[j], DT[i]) > 0 )
{
strcpy(temp, DT[j]);
strcpy(DT[j], DT[i]);
strcpy(DT[i], temp);
}//end if
}//end for
}//end for
printf("Strings in sorted order are : ");
for (int i=0; i<n; i++)
{
printf("\n String %d is %s", i+1, DT[i]);
}
getchar();
getchar();
return 0;
}//end main()
The output I get is:
Strings in sorted order are :
String 1 is A
String 2 is A
String 3 is 2vid Roy DT06 DT265A
String 4 is Roy DT06 DT265A
String 5 is Charles Wilson DT10 DT265A
String 6 is
String 7 is Chris Philips DT11 DT265A
String 8 is 06 DT265A
String 9 is avidRoy DT06 DT265A
String 10 is mith DT01 DT265David Roy Dyy DT06 DT265A
String 11 is y DT06 DT265A
String 12 is y DT06 DT265A
char *DT [12]not sure, but isn't it an array of literals?