Bubble Sort - Strings

Hi, I had to write a program that used bubble sort on an array of strings -- however when mine is compiled I get some real strange shit happening. I'm thinking the problem lies ~54 line mark; I'm not sure if comparing two strings with ">" operator means anything, or if it just compares the first letters. Anyway, any ideas on how to get this thing working? Thanks

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70

// Uses bubble sort to sort an array into
// non-descending order. (Non-descending order is basically
// ascending order but allows duplicates).

#include <stdio.h>
#include <string.h>
#define MAXELEMENTS 10
#define MAXSTRING   20

void readArray(char string[][MAXSTRING], int size);
void sortArray(char string[][MAXSTRING], int size);
void printArray(char string[][MAXSTRING], int size);



int main(int argc, char * argv[]){
    char text[MAXELEMENTS][MAXSTRING];

    printf("Please enter %d strings:\n",MAXELEMENTS);
    readArray(text,MAXELEMENTS);
    sortArray(text,MAXELEMENTS);
    printf("The sorted array:\n");
    printArray(text,MAXELEMENTS);

    return 0;
}

//Reads in exactly size numbers into the given array
void readArray(char string[][MAXSTRING],int size){
    int i;
    for(i=0; i < size; i++){
        fgets(string, MAXSTRING, stdin);
    }
}

// This function sorts the given array using bubble sort
void sortArray(char string[][MAXSTRING], int size){
    int i;
    char swap[MAXSTRING];
    int unsortedLen=size, swapped=1;


   //If we went through the whole for loop without swapping
   //the array is in order and we can stop sorting
   while ( swapped ) {
      swapped=0;
      //We go only up to unsortedLen -1
      //otherwise
      for ( i =0; i < unsortedLen -1 ; i++){
         if ( string[i] > string[i+1] ){
            strcpy(swap, string[i]);
            strcpy(string[i], string[i+1]) ;
            strcpy(string[i+1], swap);
            swapped=1;
         }
      }
      unsortedLen = unsortedLen - 1;
   }

}

//Prints an array of integers of a given size
void printArray(char string[][MAXSTRING], int size){
    int i;
    //Print out the array
    for(i=0;i< size; i++){
        printf("%s\n",string[i]);
    }
}
Last edited on
One problem you have is in the following snippet:
1
2
3
4
5
6
void readArray(char string[][MAXSTRING],int size){
    int i;
    for(i=0; i < size; i++){
        fgets(string, MAXSTRING, stdin);
    }
}

My compiler reports an error on the fgets() line:
main.c||In function ‘readArray’:|
main.c|33|error: passing argument 1 of ‘fgets’ from incompatible pointer type|
stdio.h|628|note: expected ‘char * restrict’ but argument is of type ‘char (*)[20]’|

The variable string is a pointer to an array of strings, not a single string, fgets() requires a pointer to a single string not the array. Try string[i].

And yes you can't use the comparison operators to compare c-strings you need to use strcmp() to compare the strings.

Topic archived. No new replies allowed.