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:

http://www.asciitable.com/

For the input:
1JKJ'pz'{ol'{yhklthyr'vm'{ol'Jvu{yvs'Kh{h'Jvywvyh{pvu5

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

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:

#include

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);
      }
    }
    printf("\n");
  }

  return 0;
}

No comments:

Post a Comment