0

I have to make void function where I sort the items in array according to ASCII values. And I have to change during comparing all upper letters to lower letters.

I wrote a code where I sort the items of the array:

int compareWords(const void *a, const void *b) {
    const char *first = *(const char **) a;
    const char *second = *(const char **) b;
    return strcmp(first , second);
}



void sortWsmallLetters(char **listOfWor, int count) {
    qsort(listOfWor, count, sizeof(char *), compareWords);

}

But I have no idea how to change those upper letters to lower letters in array.

S.S. Anne
  • 15,171
  • 8
  • 38
  • 76
dfps12
  • 75
  • 5
  • 6
    Are you telling us that the `compareWords` function is required to _modify_ the strings? What institution is offering this course you are taking? I would like to recommend others avoid it. Regarding a case-insensitive comparison, maybe [start here](https://stackoverflow.com/a/11685) – paddy Oct 20 '19 at 22:21

2 Answers2

0

In POSIX, this is trivial. You can use strcasecmp:

#include <strings.h>

int compareWords(const void *a, const void *b) {
    return strcasecmp(a, b);
}

void sortWsmallLetters(char **listOfWor, int count) {
    qsort(listOfWor, count, sizeof(char *), compareWords);
}

For other platforms, you'll have to roll your own (or use Windows's stricmp). Here's my implementation with a few changes to meet the requirements:

#include <ctype.h>

int strcasecmp(const char *str1, const char *str2)
{
    const unsigned char *s1 = (const unsigned char *)str1,
                        *s2 = (const unsigned char *)str2;
    while(*s1 && *s2)
    {
        if(tolower(*s1) != tolower(*s2))
            return tolower(*s1) - tolower(*s2);
        s1++; s2++;
    }

    return 0;
}

If you want to modify the strings inside your function... you really shouldn't, but you can...

#include <ctype.h>

int strcasecmp_modify(const char *str1, const char *str2)
{
    const unsigned char *s1 = (const unsigned char *)str1,
                        *s2 = (const unsigned char *)str2;
    while(*s1 && *s2)
    {
        *s1 = tolower(*s1);
        *s2 = tolower(*s2);
        if(*s1 != *s2)
            return *s1 - *s2;
        s1++; s2++;
    }

    return 0;
}
S.S. Anne
  • 15,171
  • 8
  • 38
  • 76
-1

Capital letters in ASCII are in the range 65-90, 65 being 'A', 66 being 'B' etc. Lowercase letters are in the range 97-122, 97 being 'a', 98 being 'b', etc.

Hence, in order to convert from capital to lowercase, you need to add 32 ('a' - 'A') to the capital letter's ASCII equivalent.

Here is a function that takes a char array as input and converts each uppercase letter to lowercase:

void toLower(char string[]) {
    // Looping through each character in string
    for (int i = 0; i < strlen(string); i++) {
        // Checking if character is a capital letter (range 65-90)
        if (string[i] <= 'Z' && string[i] >= 'A') {
            //Add difference between lowercase and uppercase.
            string[i] += 'a' - 'A';
        }
    }
}
  • 3
    In ASCII, `'A' - 'a'` is not 31. For that matter, it is not 32 either. And the C standard library has a function for converting to lowercase; it should not be done this way. – Eric Postpischil Oct 20 '19 at 23:00