0

I'm a beginner in C and currently in college. I got a task to make a simple alphabet decoder. I need to input 10 numbers in between 1 and 26, each number representing a letter of the alphabet. So 1 is A, 2 is B,... Z is 26.

    int c1, c2, c3, c4, c5, c6, c7, c8, c9, c10;
    
    scanf("%d %d %d %d %d %d %d %d %d %d", &c1, &c2, &c3, &c4, &c5, &c6, &c7, &c8, &c9, &c10);

I wrote this for the input, and now I was thinking of making a switch statement for each number, but it seems inpractical to have 10 switch statements. Is there any way for me to somehow make a switch statement that repeats for each input number in consecutive order?

11
  • 2
    Use an array to map numbers 1-26 (subtract 1 for array index) to letters A-Z. You definitely do not want to use a lengthy switch here (unless that is explicitly the requirement). Commented Nov 10, 2021 at 22:13
  • Are you allowed to use an array? Commented Nov 10, 2021 at 22:18
  • 1
    @Buc Fair warning: For better or worse, downvoting is how Stack Overflow handles "bad" questions. Nothing you can do about it. It doesn't necessarily mean you're a bad person for asking, so don't take it personally. Commented Nov 10, 2021 at 22:21
  • 1
    I'm still a bit confused ... you're supposed to input 10 numbers and print their letter equivalents, and you have to use a switch statement? The target/requirements keep moving in the comments also .. "not allowed to use functions", if, switch, and loops are allowed. The answer you accepted is forcing a switch statement, no one would ever write code like that. Can you please edit your question and add the assignment in its entirety? Commented Nov 10, 2021 at 22:37
  • 1
    @yano The assignment itself was very poorly worded, so I had some trouble translating it since it's not originally in English. I just contacted the professor and he approved arrays, so I did it with a for loop that goes through the array of inputs. Commented Nov 10, 2021 at 23:11

1 Answer 1

1

Assuming ASCII encoding, you can convert an integer in the range 1 to 26 to a letter from 'A' to 'Z' this way:

char a1 = 'A' + (c1 - 1);

You can then print the letter with

putchar(a1);

or store it into an array along with the other ones.

If you are required to use a switch statement, you can abuse the rule this way:

int int2char(int c) {
    if (c >= 1 && c <= 26)
        switch (c) { default: return 'A' + (c - 1); }
    else
        return '?';
}

Or this way (less likely to cause a warning):

int int2char(int c) {
    switch (c >= 1 && c <= 26) {
      case 1: return 'A' + (c - 1);
      default: return '?';
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

This might suffice if a machine grades the assignment and it simply needs to find the keyword switch in the source code (and get the right output for given inputs). I'd be surprised if this would pass muster with the professor, however (assuming switch is indeed an absolute requirement).
@jarmod: If I were the professor, I would award a badge best abuse of the rules, in reference to the IOCCC, but I am sure more abusive solutions can be found :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.