0

I have a string of different 3 letter words:

catdogmattabratliematdogatt

I need to assign each of the words with an alphabet and print the final string.

cat: P, dog: A, mat: T, tab: I, rat: E, lie: O, att: L

so my output should be:

PATIEOTAL

I tried using a switch statement but it doesn't allow me to enter 3 characters together.

snazziii
  • 471
  • 2
  • 10
  • 23
  • Maybe this will help: http://stackoverflow.com/questions/11995568/how-do-you-have-logical-or-in-case-part-of-switch-statment/11995579#11995579 – jrok Nov 20 '12 at 22:12
  • This may be of more help: http://stackoverflow.com/questions/1494399/how-do-i-search-find-and-replace-in-a-standard-string – Chad Nov 20 '12 at 22:15

3 Answers3

4

Use a map<string, char> to do the following:

map<string, char> wordToChar;
// Load wordToChar appropriately; c++11 you can use an initializer list for simplicity

for (int i = 0; i < strLen; i += 3)
{
  string str;
  str.push_back(theString[i]);
  str.push_back(theString[i+1]);
  str.push_back(theString[i+2]);
  cout << wordToChar[theString] << endl;
}
Rollie
  • 4,391
  • 3
  • 33
  • 55
  • i tried using map but it sorts my keys alphabetically which does not give the required output – snazziii Nov 20 '12 at 22:18
  • 2
    @sumrania: How would sorted keys cause a problem? – Jerry Coffin Nov 20 '12 at 22:20
  • I just added a new word correlation where att: L. When using a map, it first substitutes all atts and then cat and then dog. so my output becomes: PAmLabEOAL – snazziii Nov 20 '12 at 22:23
  • 1
    @Rollie: One more possibility: you could do: `string str(theString+i, 3);` instead. Also, `map` doesn't overload `operator()`, it overloads `operator[]`, so you want `cout << wordToChar[str];` (and beware that it'll print an empty string (and insert it into the map) for any input that doesn't match one you've defined). – Jerry Coffin Nov 20 '12 at 22:24
  • @sumrania that is likely more an issue with your replacement algorithm - can you post the code you are using? – Rollie Nov 20 '12 at 23:40
  • i figured it out. You were right I was using replace and the map operator. thank you very much!! – snazziii Nov 20 '12 at 23:43
0

It seems I can use three char in a switch-statement! It is also not just me but a standard features called "multicharacter literals" (see 2.14.3 [lex.ccon] paragraph 1; the type of these beasts is int). It isn't that I would advise anybody to use this hack but it works although the characters may need to be reversed in the computation depending on the Endianess of the system (I'm not sure about that detail). Here is a complete example (although the input wants separate words rather than one string which needs to be separated):

#include <iostream>
#include <string.h>

int main(int ac, char* av[])
{
    typedef unsigned char u;
    for (int i(1); i != ac; ++i) {
        if (strlen(av[i]) == 3) 
        {
            std::cout << std::hex;
            int value(u(av[i][2])
                      + 256u * (u(av[i][1])
                                + 256u * u(av[i][0])));
            switch (value) {
            default:
                std::cout << "option not found!: '" << av[i] << "'\n";
                break;
            case 'cat': std::cout << 'P'; break;
            case 'dog': std::cout << 'A'; break;
            case 'mat': std::cout << 'T'; break;
            case 'tab': std::cout << 'I'; break;
            case 'rat': std::cout << 'E'; break;
            case 'lie': std::cout << 'O'; break;
            case 'att': std::cout << 'L'; break;
            }
        }
    }
    std::cout << '\n';
}
Dietmar Kühl
  • 150,225
  • 13
  • 225
  • 380
0

Since you asked about using switch statements, I assume that it would be acceptable to have a hard-coded list of 3-letter words and their corresponding letters. In that case, I would solve this problem using a sequence of if-then-else statements, each attempting to match the characters of the 3-letter words. Alternatively, you could use nested switch statements, but the syntax makes that solution a bit harder to read IMO.

static char match_word(std::string const &str, std::size_t offset)
{
  char ret = '?';
  if (str[offset + 0] == 'c' && str[offset + 1] == 'a' && str[offset + 2] == 't') {
    ret = 'P';
  }
  else if (str[offset + 0] == 'd' && str[offset + 1] == 'o' && str[offset + 2] == 'g') {
    ret = 'A';
  }
  else if (str[offset + 0] == 'm' && str[offset + 1] == 'a' && str[offset + 2] == 't') {
    ret = 'T';
  }
  else if (str[offset + 0] == 't' && str[offset + 1] == 'a' && str[offset + 2] == 'b') {
    ret = 'I';
  }
  else if (str[offset + 0] == 'r' && str[offset + 1] == 'a' && str[offset + 2] == 't') {
    ret = 'E';
  }
  else if (str[offset + 0] == 'l' && str[offset + 1] == 'i' && str[offset + 2] == 'e') {
    ret = 'O';
  }
  else if (str[offset + 0] == 'a' && str[offset + 1] == 't' && str[offset + 2] == 't') {
    ret = 'L';
  }
  return ret;
}

Then you can test the code with a simple main function like this:

int main(int argc, char **argv)
{
  if (argc != 2) {
    std::cerr << "USAGE: " << argv[0] << " ENCODED" << std::endl;
    return 1;
  }
  else {
    std::string example(argv[1]);
    for (std::size_t idx = 0; idx < example.size(); idx += 3) {
      std::cout << match_word(example, idx);
    }
    std::cout << std::endl;
    return 0;
  }
}

Then just run the program with the encoded string as the one and only argument like this:

$ ./a.out catdogmattabratliematdogatt
PATIEOTAL
dsh
  • 31
  • 1
  • 5