Need help with Sinhala Unicode and C programming

Safari7

Banned
Apr 18, 2014
232
8
0
I can print sinhala words to the console. But can't print letter by letter!

This prints the word correctly to the console:

Code:
char *word = "අලංකෘත";
    
    // print the word
    printf("%s\n",word);


But the below code doesn't print characters correctly.

Code:
char *word = "අලංකෘත";
        
    // print letter by letter
    int n = sizeof(word) / sizeof(word[0]);
    
    for (int i = 0; i < n; i++) {
        printf("%c\n", word[i]);
    }

I get the following output from the above code.

Code:
\340
\266
\205
\340
\266
\275
\340
\266


My goal is to read a text file that has a large number of sinhala words and convert them to equivalent phonetic english words. For this, I need to compare letters in the sinhala unicode word. So that means I need a way to read letter by letter.

I know a very little about Unicode stuff. So any tip/advice would be much appreciated!
 

Arkham Knight

Member
Mar 14, 2014
1,627
89
0
මේක ගහලා බලන්න

Code:
#include <stdio.h>
#include <locale.h>
#include <wchar.h>

main()
{

wchar_t *word = L"අලංකෘත";
        
    // print letter by letter
    int n = sizeof(word) / sizeof(word[0]);
    int i;
    
    for(i=0; i < n; i++) 
    {
        wprintf(L"%c\n", word[i]);
    }

}
 

Arkham Knight

Member
Mar 14, 2014
1,627
89
0
එකක් කියන්න ඔනේ උබ අමතක කරලා wide characters use කරන්න.
printf(), char වගේ ඒවා හරියන්නේ ASCII වලට විතරයි unicode use කරනවා නම් wide characters වලට support කරන keyword use කරන්න ඕනේ එතකොට unicode ගහන්න පුලුවන්.
 

Safari7

Banned
Apr 18, 2014
232
8
0
එකක් කියන්න ඔනේ උබ අමතක කරලා wide characters use කරන්න.
printf(), char වගේ ඒවා හරියන්නේ ASCII වලට විතරයි unicode use කරනවා නම් wide characters වලට support කරන keyword use කරන්න ඕනේ එතකොට unicode ගහන්න පුලුවන්.

Code:
\205
\275
Program ended with exit code: 0

තාම ප්‍රින්ට් වෙන්නෙ පරන විදියට. Wide characters support ගැන කිව්වට තෑන්ක්ස්. ඒ ගැන කියවන්න ඕන වෙලාවක.

ෆයිල් එක read කරගන්න මම පයිතන් වලින් කෝඩ් එක ලියාගත්ත. C වලින් කරන හැටි දන්න කෙනෙක් ඉන්නවනම් මෙතන කියන්න. පස්සෙ බලන කාට හරි උදව්වක් වෙයි.

Code:
#!/usr/bin/python
# -*- coding: utf-8 -*- 

import codecs

def process_word(word):
        #print "word is", word
        
        for c in word:
        	print c
        
# print word character by character
process_word(u'අලංකෘත')

# read the text file and print all words character by character        
with codecs.open('si.txt','r','utf-8') as f:        
     for row in f:
         Data = row.split()
         try:
            word = Data[0]
            #print word
            #process_word(word)
         except IndexError:
            print 'You have an empty row'