简体   繁体   English

如何在C中返回ascii char的索引

[英]How to return the index of ascii char in C

So you have Table mapping the 26 ascii characters from the English alphabet to their corresponding morse code strings 因此,您可以使用Table将英文字母中的26个ascii字符映射到它们相应的莫尔斯电码字符串

typedef struct a_look_tab {
    char table[asciiNum][MORSE_MAX+1]; 

} ALookTab;

and asciiNum is the 0 for a, 1 for b, and so on. asciiNum是a的0,b的1,依此类推。 how to return an index (int) that is the index of the morse char. 如何返回作为莫尔斯字符的索引的索引(int)。

So what we are doing after converting a char into a number is to param ascii The ascii character to convert and return The index for the given ascii character, how do we do that? 因此,将char转换为数字后,我们要做的就是将ascii参数转换为ascii字符并返回给定ascii字符的索引,我们该怎么做?

The simplest portable way to convert the char to an index is this type of construction: 将char转换为索引的最简单的可移植方式是这种类型的构造:

/* Returns -1 if c is not an upper- or lower-case alphabetical character */
int char_to_index(char c)
{
    static const char * const alphabet = "abcdefghijklmnopqrstuvwxyz";
    char *p = strchr(alphabet, tolower((unsigned char)c));

    return p ? p - alphabet : -1;
}

You need to convert a character, such as 'a', into its index in the table. 您需要将一个字符(例如“ a”)转换为表中的索引。 According to your specification, the table begins with the Morse code for 'a', so 'a' should map to the index 0, 'b' should map to 1, and so on. 根据您的规范,该表以“ a”的莫尔斯电码开头,因此“ a”应映射到索引0,“ b”应映射到1,依此类推。

The simplest such mapping could be implemented like this: 最简单的映射可以这样实现:

int char_to_index(char c)
{
  return tolower(c) - 'a';
}

This subtracts the ASCII code for 'a' from the given letter, which will turn 'a' into 0, and so on. 这将从给定字母中减去“ a”的ASCII码,这会将“ a”变为0,依此类推。

Unfortunately, this only works if the computer running the program encodes the letters of the alphabet using a system that assigns contiguous codes to the letters. 不幸的是,这仅在运行程序的计算机使用将连续代码分配给字母的系统对字母进行编码的情况下有效。 Not all computers are like this. 并非所有计算机都是这样。 A more portable function could do the mapping explicitly, like so: 更具移植性的函数可以显式地进行映射,如下所示:

int char_to_index2(char c)
{
  switch(tolower(c))
  {
  case 'a': return 0;
  case 'b': return 1;
  case 'c': return 2;
  /* and so on */
  }
}

This is more verbose code-wise, but more portable. 这在代码方面更为冗长,但更易于移植。

UPDATE: I added calls to tolower() to both functions to make them a bit more robust. 更新:我向两个函数添加了对tolower()调用,以使它们更加健壮。

Note that the C standard doesn't require ASCII, and this code won't work under EBCDIC, but 99% of the time this won't matter. 请注意,C标准不需要ASCII,并且该代码在EBCDIC下不起作用,但是99%的时间都无关紧要。

I believe what you're looking for is much simpler than you think. 我相信您要寻找的东西比您想的要简单得多。 Character literals like 'c' and '0' are actualy int s, not char s - they're casted down to char at assignment, and can be just as easily cast back up. 字符文字像'c''0'是actualy int S,不char秒-他们正在铸造到char的分配,并可以很容易地投回来了。 So this is what (I think) you want: 这就是您想要的(我认为):

#include <ctype.h> // for tolower()

char *func(ALookTab *a, char c)
{
    if(isalpha(c))
        return a->table[tolower(c) - 'a'];
    if(isdigit(c))
        return a->table[c - '0' + 26];
    // handle special characters
}

Note that this code assumes that your morse code is stored as the 26 alphabet characters, the 10 digits, and then other special characters in whatever order you choose. 请注意,此代码假定您的摩尔斯电码以您选择的任何顺序存储为26个字母字符,10个数字以及其他特殊字符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM