0

I need to convert a char array to a lower case char array. I also want to convert some special characters like Ä to ä. But the "ASCII-Code" 196 isn't in the ASCII-128. How can I convert them to lower case ? I can use them as string initializer but can't really deal with their codes. In case this might be a compiler option I'm using eclipse CDT on Linux without c99 mode (can't use that one).

char* toLowerCase(char* text)
{
    int length = strlen(text);
    char* result = malloc(length); // malloc adds the 0 automatically at result[length]
    int i;
    for (i = 0; i < length; i++)
        if ((text[i] >= 65) && (text[i] <= 90))
            result[i] = text[i] | 32;
        else
            result[i] = text[i];
    return result;
}

toLowerCase("hElLo WoRlD ÄäÖöÜü");
// result is "hello world ÄäÖöÜü"

tolower() from ctype.h doesn't do it either.

7
  • 3
    Apart from being utterly non-portable, this further extends the misery by being utterly non-high-asci friendly. And you can't use a compiler based on a standard thats only 14 years old? I bet your prof is real popular. Commented Jan 25, 2013 at 10:22
  • 1
    Your return string lacks termination. Commented Jan 25, 2013 at 10:25
  • 4
    Please read The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!) before you go any further dealing with characters. Commented Jan 25, 2013 at 10:25
  • @WhozCraig My prof was pretty popular. But that's not the point and I'm not a lazy student. Despite that I'm also unhappy about it I'm bound to this restrictions. I just solved it on my own anyways. Commented Jan 25, 2013 at 10:42
  • Excellent! "was" popular? I hope there isn't a sad end to that story. Anyway, very good for solving it. grats! Commented Jan 25, 2013 at 10:53

1 Answer 1

3

I think you need to read about setlocale, (or here)

and use tolower()

Notes: During program startup, the equivalent of setlocale(LC_ALL, "C"); is executed before any user code is run.

You can try the Environment's default locale (or select the correct if you know it):

setlocale(LC_ALL, "");
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.