Answer to Question #300700 in C++ for adi

Question #300700

The tap code, sometimes called the knock code, is a way to encode text messages on a letter-by- letter basis in a very simple way. Tap code has been one of the most basic communication protocols and still used to convey SOS messages and other urgent communication. The tap code uses a 5×5 grid of letters representing all the English alphabets, see Figure 1. To communicate the word "water", the cipher would be the following (with the pause between each number in a pair being shorter (single space) than the pause between letters (two spaces)),

Your task is to design a program that can

i) convert any given string into a Tap code sequence

Prototype: char* convertToTapCode(char*)

ii) and A Tap code sequence to a string (char*)

Prototype: char* convertToString(char*)


Note: You are not authorized to use string data type; however, you can use char*


1
Expert's answer
2022-02-23T00:10:55-0500
#include <stdlib.h>
#include <malloc.h>
#include <stdio.h>

char *convertToTapCode(char *seq) {
  size_t sz = strlen(seq);
  char *code = malloc(sizeof(char) * 2 * sz);
  for (int i = 0, j = 0; i < sz; i++) {
    int n = seq[i] - 'A';
    code[j++] = 'A' + n / 5;
    code[j++] = 'A' + n % 5;
  }
  return code;
}

char convertToString(char *code) {
  size_t sz = strlen(code);
  char *seq = malloc(sizeof(char) * sz / 2);
  for (int i = 0, j = 0; i < sz; i += 2) {
    seq[j++] = 5 * code[i] + code[i+1] - 'A';
  }
  return seq;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog