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

问题描述

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



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

  

EDIT 您说: 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中的重音/变音字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 02:46