1

I'm trying to sort an array of strings into an ascending order

Abcdefg
Ultimate
UMN 
Bawaw

i expect the result should be into:

Abcdefg
Bawaw
Ultimate
UMN

I found that bubble sort is sorting strings like :

Abcdefg
Bawaw
UMN
Ultimate

And when i try to bypass it, by turning everything into an uppercase / lowercase strings, but if i add the data, it became juggled

#include<stdio.h>
#include<string.h>

int main(int argc, char **argv){

    char data[8][50] = {"Abcdefg","Ultimate","UMN", "Bawaw", "Ultima Sonora", "UMN Medical Centre", "Ultima Toys","BACD"};
    char temp[10];
    int i, j;

    printf("Original data = \n");
    for(i=0; i<8; i++){
        printf("%s\n", data[i]);
    }

    printf("\n");
    for(i = 0; i < 8; i++){
        for(j = 0; j < 50; j++){
            if(data[i][j] >= 65 && data[i][j] <= 90){
                data[i][j] += 32;
            }
        }
    }


    for(i = 0; i <8 ; i++){
        for(j = i+1; j < 8; j++)
        if(strcmp(data[i], data[j])>0){
            strcpy(temp, data[i]);
            strcpy(data[i],data[j]);
            strcpy(data[j], temp);
        }
    }
    printf("Sorted data = \n");
    for(i=0; i<8; i++){
        printf("%s\n", data[i]);
    }
    return 0;
}

Do anyone have a clue what's going on there?

  • 1
    Aside: please use `tolower()` to be found in `ctype.h`. You don't even need to test the range first. `data[i][j] = tolower(data[i][j]);` – Weather Vane Apr 06 '20 at 10:20
  • 1
    You have string overflow. In the first test `char temp[10];` was OK to hold `"Ultimate"`, but now is too short to hold `"UMN Medical Centre"`. I suggest `char temp[50];` to match the 2D array's strings. – Weather Vane Apr 06 '20 at 10:22
  • 2
    You might want to check if you library supports `strcmpi` - the case insensitive equivalent to `strcmp`. - this is more efficient than turning the whole string into upper or lower case. – 500 - Internal Server Error Apr 06 '20 at 10:25
  • @WeatherVane Thanks for the answer! I really appreciate it. Do you mean by the range is the number of characters in the array? – Christian Halim Apr 06 '20 at 12:18
  • By "range" I mean the character values. You can safely pass `'2'` to `tolower` and it will return the same value - not some wierd attempt to lowercase it. – Weather Vane Apr 06 '20 at 12:20
  • @WeatherVane ahh okay, convenient. I wonder is there also to-'uppercase' function? I can't seem to find it. – Christian Halim Apr 06 '20 at 12:38
  • @500-InternalServerError Okay, will do. Thankss – Christian Halim Apr 06 '20 at 12:41
  • The uppercase function is somewhat unsurprisingly called `toupper()`. You might also be interested a family of functions called `isupper()` and `isdigit()` and `isalpha()` and so on. – Weather Vane Apr 06 '20 at 12:50

2 Answers2

3

first note that temp doesn't have enough space to hold some of your strings , so use char temp[50]. also since ASCII code of l is smaller than m the result you are excepting is true , so is your program , but since you have overflow ,the program will have undefined behavior. which means anything can happen.

Read this link How dangerous is it to access an array out of bounds?also it's not bad to take look here how strcmp() works

hanie
  • 1,863
  • 3
  • 9
  • 19
  • thanks hanie for showing up again, I really appreciate it. I'll also read your link recommendation well. :) – Christian Halim Apr 06 '20 at 12:17
  • @ChristianHalim your welcome,since I'm student and I have always found help with my questions here , feel free this is purpose of SO . – hanie Apr 06 '20 at 12:27
1

One more thing i see in your code is in the part of uppercase / lowercase strings.

for(i = 0; i < 8; i++){
        for(j = 0; j < 50; j++){
            if(data[i][j] >= 65 && data[i][j] <= 90){
                data[i][j] += 32;
            }
        }
    }

you should use j < strlen(data[i]. You can also use tolower function as the comment above.

It becomes:

for(i = 0; i < 8; i++){
        for(j = 0; j < strlen(data[i]); j++){
            data[i][j] = tolower(data[i][j]);
        }
    }
Hitokiri
  • 3,607
  • 1
  • 9
  • 29
  • Thanks for showing again to help me, & sorry for the late reply. I really appreciate you for always recommending useful functions for my code, cheers :)) – Christian Halim Apr 06 '20 at 12:11