Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
178 views
in Technique[技术] by (71.8m points)

Alternative to this switch statement in C

The code which I posted below is a part of my whole program. The rest of the code is not related to this question. Is there any solution other than using this long switch statement? The main objective is to shorten the code. The if statement is used simply to capitalize lowercase letters.

int POINTS[] = { 1, 3, 3, 2, 1, 4, 2, 4, 1, 8, 5, 1, 3, 1, 1, 3, 10, 1, 1, 1, 1, 4, 4, 8, 4, 10 };

int compute_score(string word)
{
  // TODO: Compute and return a score for string
  int word_length = strlen(word);
  int i, sum = 0;

  for (i = 0; i < word_length; i++)
  {
    //This if statement is used to captalise lower-case letters.
    if (islower(word[i]))
    {
      word[i] = toupper(word[i]);
    }

    //This switch statement is the key in this program. It will check 
    //each letter of word and then modify variable sum according to it.
    switch (word[i])
    {
    case 'A':
      sum = sum + POINTS[0];
      break;
    case 'B':
      sum = sum + POINTS[1];
      break;
    case 'C':
      sum = sum + POINTS[2];
      break;
    case 'D':
      sum = sum + POINTS[3];
      break;
    case 'E':
      sum = sum + POINTS[4];
      break;
    case 'F':
      sum = sum + POINTS[5];
      break;
    case 'G':
      sum = sum + POINTS[6];
      break;
    case 'H':
      sum = sum + POINTS[7];
      break;
    case 'I':
      sum = sum + POINTS[8];
      break;
    case 'J':
      sum = sum + POINTS[9];
      break;
    case 'K':
      sum = sum + POINTS[10];
      break;
    case 'L':
      sum = sum + POINTS[11];
      break;
    case 'M':
      sum = sum + POINTS[12];
      break;
    case 'N':
      sum = sum + POINTS[13];
      break;
    case 'O':
      sum = sum + POINTS[14];
      break;
    case 'P':
      sum = sum + POINTS[15];
      break;
    case 'Q':
      sum = sum + POINTS[16];
      break;
    case 'R':
      sum = sum + POINTS[17];
      break;
    case 'S':
      sum = sum + POINTS[18];
      break;
    case 'T':
      sum = sum + POINTS[19];
      break;
    case 'U':
      sum = sum + POINTS[20];
      break;
    case 'V':
      sum = sum + POINTS[21];
      break;
    case 'W':
      sum = sum + POINTS[22];
      break;
    case 'X':
      sum = sum + POINTS[23];
      break;
    case 'Y':
      sum = sum + POINTS[24];
      break;
    case 'Z':
      sum = sum + POINTS[25];
      break;
    }
  }
  return sum;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I'm guessing that you want something like this and simply haven't bothered to set all the indices in your switch statement correctly:

if ('A' <= word[i] && word[i] <= 'Z') {
    sum += POINTS[word[i] - 'A'];
}

The trick is that chars are just numbers as well, so you can compare them to see if the character is an uppercase Latin letter, and subtract 'A' to get the 0-based position in the POINTS array.

However, note that this only works if the character set on your target system contains the letters A through Z contiguously in alphabetical order. By far most systems these days use (a superset of) ASCII, where the above code works fine. But if a more exotic character set such as EBCDIC is used, you will need a different approach, such as a lookup table, a hash table, or... a big switch statement.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...