Hi,

I have a problem with the tolower function. I use Netbeans 6.5 with GCC C++ on Ubuntu 64bit. The problem is, that I use nordic letters like å, ä and ö and the tolower function does not affect them at all. I have tried almost everything. I have tried to replace char by char, like this:

for(unsigned int i = 0; i < strToConvert.length(); i++)
    {
        if (strToConvert[i] == 'Ö')
            strToConvert[i] = 'ö';
        else
            strToConvert[i] = tolower(strToConvert[i]);
    }
    return strToConvert;//return the converted string

This won't work! I have also tried to use wstring and transform but no luck. Setting the locale does nothing. I'm prepared to write a custom function, I just need some ideas on how to get around this.

Also, if I print the codes for the letters with:

string str = "ÅÄÖRNS" //an example
for (int i = 0; i < str.length(); i++) {
     cout << (int) str[i] << endl;
}

'r' will print 114, 'a' 97 as expected. But 'Ö' for example will print two number: -61 and -106, 'Ä' prints -61 and -124. What's up with that?


Thanks,
Mattias

Recommended Answers

All 4 Replies

tolower() doesnt work on Ä , Ö or Å. I think you have to do an extra check if the letter is one of those ( I'd say use switch-case )

EDIT: sorry, you already did that :D

Anyways make a function which you call for each character individually, dont just run the for-loop for the string.

tolower() doesnt work on Ä , Ö or Å. I think you have to do an extra check if the letter is one of those ( I'd say use switch-case )

EDIT: sorry, you already did that :D

Anyways make a function which you call for each character individually, dont just run the for-loop for the string.

I have tried that. This:

if (ch == 'Ö') //309
        letter= 'ö'; //310

give compiler warnings:

func.cpp:309:15: warning: multi-character character constant
func.cpp: In function ‘char checkLetter(char)’:
func.cpp:310: warning: overflow in implicit constant conversion

Any idea?

The best so far is:

string str = "ö";
    int index = strToConvert.find_first_of('Ö');
    cout << "index: " << index << endl;
    if (index > -1) {
        string::iterator it;
        it = strToConvert.begin() + index;
        strToConvert.erase(it);
        strToConvert.insert(index, str);
    }

Which prints out this: �ö
I don't know where question mark comes from.

/Mattias

Solved!

string str = "ö";
    int index = strToConvert.find_first_of('Ö');
    if (index > -1) {
        cout << "index: " << index << endl;
        string::iterator it;
        it = strToConvert.begin() + index;
        strToConvert.erase(it);
        strToConvert.insert(index, str);
        it = strToConvert.begin() + index - 1;
        strToConvert.erase(it);
    }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.