Thread: Quick question about characters

  1. #1
    Registered User
    Join Date
    Apr 2007
    Location
    Sweden
    Posts
    12

    Quick question about characters

    I'm trying to replace a string containing the characters 'åäö'. It's easy to std::cout them directly to the screen, replacing them was a bit trickier though. This is my solution:

    If you can't see which characters I mean, it's character (134,132,148)
    http://www.asciitable.com/

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main(void) {
    	string str = "0123456789 abcdefghijklmnopqrstuvxyz åäö";
    
        str.replace(str.find("å"), str.find("å")+1, "\x86");
    	str.replace(str.find("ä"), str.find("ä")+1, "\x84");
    	str.replace(str.find("ö"), str.find("ö")+1, "\x94");
        str.replace(str.find("Å"), str.find("Å")+1, "\x8F");
    	str.replace(str.find("Ä"), str.find("Ä")+1, "\x8E");
    	str.replace(str.find("Ö"), str.find("Ö")+1, "\x99");
    	
    	for (int i=0;i<str.size();i++) {
    		cout << str[i];
    	}
    
        cin.ignore(cin.rdbuf()->in_avail()+1);
        cin.get();
        return 0;
    }
    How would you do, if you wanted to replace the string like I want to. It works sometimes which is quite weird. Right now I only get run-time errors though.

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    I'd use wide strings. C++ is unfortunately very unprepared for dealing with international data. Wide characters and strings work somewhat, but they're not ideal. Still, they're the best solution you'll get without involving 3rd party libraries or lots of specialized code.

    Make sure that you know the encoding of your file, and that your compiler knows it, too.
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main(void) {
    	wstring str = L"0123456789 abcdefghijklmnopqrstuvxyz &#229;&#228;&#246;";
    
        // You might want to call find() only once and cache the result. You also absolutely HAVE TO
        // verify that you actually found something. That's most likely the source of your errors.
        // Oh, and you ought to use proper values for the replacement.
        str.replace(str.find(L"&#229;"), str.find(L"&#229;")+1, L"\x86");
    	str.replace(str.find(L"&#228;"), str.find(L"&#228;")+1, L"\x84");
    	str.replace(str.find(L"&#246;"), str.find(L"&#246;")+1, L"\x94");
        str.replace(str.find(L"&#197;"), str.find(L"&#197;")+1, L"\x8F");
    	str.replace(str.find(L"&#196;"), str.find(L"&#196;")+1, L"\x8E");
    	str.replace(str.find(L"&#214;"), str.find(L"&#214;")+1, L"\x99");
    	
    	for (int i=0;i<str.size();i++) {
    		wcout << str[i];
    	}
    
        wcin.ignore(wcin.rdbuf()->in_avail()+1);
        wcin.get();
        return 0;
    }
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by password View Post
    If you can't see which characters I mean, it's character (134,132,148)
    http://www.asciitable.com/
    What an awful web page. The character set shown is PC-8, not "Extended ASCII." There is no such thing as Extended ASCII.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. quick question (C/C++)
    By owi_just in forum C Programming
    Replies: 2
    Last Post: 03-19-2005, 09:44 AM
  2. quick question
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 07-22-2002, 04:44 AM
  3. Quick Question Regarding Pointers
    By charash in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2002, 11:04 AM
  4. Quick Question on File Names and Directories
    By Kyoto Oshiro in forum C++ Programming
    Replies: 4
    Last Post: 03-29-2002, 02:54 AM
  5. Quick question: exit();
    By Cheeze-It in forum C Programming
    Replies: 6
    Last Post: 08-15-2001, 05:46 PM