Thursday, October 9, 2014

458: Decoder

It's been a while since I tried out UVA. am currently applying for a programming job, and the recruiter said that I need to demonstrate skills in C/C++. And so I thought I may need to try out a few programming problems before the recruiter's technical exam.

uHunt recommends that I solve 458 (Decoder). The clue for this problem is the ASCII table:

For the input:

Notice that the first character is '1'. The ascii character '1' has a decimal equivalent of 49 based on

The output for the input above is

 *CDC is the trademark of the Control Data Corporation.

Therefore the input to output conversion is from '1' to '*'. The ascii character '1' has a decimal equivalent of 49. The ascii character '*' has a decimal equivalent of 42. 49-42 = 7. Therefore 7 is the decimal equivalence between input ascii character and output ascii character.

Just be careful with the the endline character at the end of each input line. What I did was to check each character in the input line. If the character is not equal to the endline, then convert it.

Here's the code:


int main() {
  char line[100];
  char c;
  int i;

  while(fgets(line, 100, stdin)) {
    for (i = 0; c = line[i]; i += 1) {
      if (c != '\n') {
        c -= 7;
        printf("%c", c);

  return 0;

No comments:

Post a Comment