C中的重音/变音字符? [英] Accented/umlauted characters in C?

查看:199
本文介绍了C中的重音/变音字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是在学习C,并得到一个任务,我们必须将纯文本转换为莫尔斯电码并回来。 (我最熟悉Java,所以我用我使用的术语。)



为了做到这一点,我有一个数组包含所有字母的字符串。 p>

  char * letters [] = {
.-,-...,-.-。 , - ..,。,..-。 etc

我写了一个函数来返回所需字母的位置。

  int letter_nr(unsigned char c)
{
return c-97;
}

这是工作,但是分配规范要求处理瑞典语变音字母åäö。瑞典字母表与英语与最后这三个字母相同。我试着检查这些,像这样:

  int letter_nr(unsigned char c)
{
if (c =='å')
return 26;
if(c =='ä')
return 27;
if(c =='ö')
return 28;
return c-97;不幸的是,当我尝试测试这个函数时,我得到相同的值,所有的值都为0。下面是我的主要测试功能:

  int main()
{
无符号字母;

while(1)
{
printf(键入一个字母以获取其位置:);
scanf(%c,& letter);
printf(%d\\\
,letter_nr(letter));
}
return 0;
}

我可以如何解决这个问题?

解决方案

在一般编码的东西是相当复杂。另一方面,如果你只是想要一个脏的解决方案特定于你的编译器/平台比添加这样的代码:

  printf(letter 0x%x is number%d \\\
,letter,letter_nr(letter));

它将为您的变音符号提供十六进制值。

EDIT 您说:你总是得到98,所以你的scanf有 98 + 97 = 195 = 0x3C 。根据此 0x3C是常见的UTF8序列的开始拉丁小邮件N与某事 c> c> 。您在 Mac OS X



EDIT 这是我的最后通话。相当的黑客,但它适用于我:)

  #include< stdio.h> 

// scanf for for letter。在莫氏表中返回位置。
//识别UTF8的瑞典字母。
int letter_nr()
{
unsigned char letter;
//第一次扫描,
scanf(%c,& letter);
if(0xC3 == letter)
{
//我们scanf再次,因为这是UTF8和两个字节编码字符将来
scanf(%c,& letter );
//拉丁小写字母A带环上面=å
if(0xA5 == letter)
return 26;
//拉丁小写字母A与DIAERESIS =ä
if(0xA4 == letter)
return 27;
//拉丁小写字母O与DIAERESIS =ö
if(0xB6 == letter)
return 28;

printf(Unknown letter。0x%x。,letter);
return -1;
}
//似乎是常规ASCII
return letter - 97;
} // letter_nr

int main()
{
while(1)
{
printf其位置:);

int val = letter_nr();
if(-1!= val)
printf(Morse code is%d.\\\
,val);
else
printf(Unknown Morse code.\\\
);

//删除剩余的新行
unsigned char new_line;
scanf(%c,& new_line);
}
return 0;
}


I'm just learning about C and got an assignment where we have to translate plain text into morse code and back. (I am mostly familiar with Java so bear with me on the terms I use).

To do this, I have an array with the strings for all letters.

char *letters[] = {
".- ", "-... ", "-.-. ", "-.. ", ".", "..-." etc

I wrote a function for returning the position of the desired letter.

int letter_nr(unsigned char c)
{
    return c-97;
}

This is working, but the assignment specifications require the handling of the Swedish umlauted letters åäö. The Swedish alphabet is the same as the English with these three letters in the end. I tried checking for these, like so:

int letter_nr(unsigned char c)
{
    if (c == 'å')
        return 26;
    if (c == 'ä')
        return 27;
    if (c == 'ö')
        return 28;
    return c-97;
}

Unfortunately, when I tried testing this function, I get the same value for all of these three: 98. Here is my main, testing function:

int main()
{   
    unsigned char letter;

    while(1)
    {
        printf("Type a letter to get its position: ");
        scanf("%c", &letter);
        printf("%d\n", letter_nr(letter));
    }
    return 0;
}

What can I do to resolve this?

解决方案

In general encoding stuff is quite complicated. On the other hand if you just want a dirty solution specific to your compiler/platform than add something like this to your code:

printf("letter 0x%x is number %d\n", letter, letter_nr(letter));

It will give hex value for your umlauts. Than just replace in if statements your letter with number.

EDIT You say that you are always getting 98 so your scanf got 98 + 97 = 195 = 0x3C from console. According to this table 0x3C is start of UTF8 sequence for common LATIN SMALL LETTER N WITH Something in Latin1 block. You are on Mac OS X ?

EDIT This is my final call. Quite hackery but it works for me :)

#include <stdio.h>

// scanf for for letter. Return position in Morse Table. 
// Recognises UTF8 for swedish letters.
int letter_nr()
{
  unsigned char letter;
  // scan for the first time,
  scanf("%c", &letter);
  if(0xC3 == letter)
  {
    // we scanf again since this is UTF8 and two byte encoded character will come
    scanf("%c", &letter);
    //LATIN SMALL LETTER A WITH RING ABOVE = å
    if(0xA5 == letter)
      return 26;
    //LATIN SMALL LETTER A WITH DIAERESIS = ä
    if(0xA4 == letter)
      return 27;
   // LATIN SMALL LETTER O WITH DIAERESIS = ö
    if(0xB6 == letter)
      return 28;

    printf("Unknown letter. 0x%x. ", letter);
    return -1;
  } 
  // is seems to be regular ASCII
  return letter - 97;
 } // letter_nr

int main()
{   
    while(1)
    {
        printf("Type a letter to get its position: ");

        int val = letter_nr();
        if(-1 != val)
          printf("Morse code is %d.\n", val);
        else
          printf("Unknown Morse code.\n");

        // strip remaining new line
    unsigned char new_line;
    scanf("%c", &new_line);         
    }
    return 0;
}

这篇关于C中的重音/变音字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆