Home > Blockchain >  How do I create a program that replaces the CODE to VALUE?
How do I create a program that replaces the CODE to VALUE?

Time:05-09

I just wanted to help with my code here's my questions

How can I input "computer" in any in any order and case insensitively and still get the correct output? Here are the replacement

COMPUTERS.X

1234567890.X

If I input other letters that is not included in the COMPUTERS.X the program will terminate and ask again if i should input again. example:

Input Code: Most.x

UNABLE TO CONVERT YOUR INPUT

INPUT AGAIN? Type YES to input again, type NO to end program:

Is there any way or method to replace the whole string array instead of replace(CODES.begin(),CODES.end(),'C','1'); here's my code

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

using namespace std;

int main()
{
    string CODES;
    char choice[5];

    do
    {
        cout << "Input code:  ";
        cin >> CODES;

    replace(CODES.begin(),CODES.end(),'C','1');
        replace(CODES.begin(),CODES.end(),'O','2');
        replace(CODES.begin(),CODES.end(),'M','3');
        replace(CODES.begin(),CODES.end(),'P','4');
        replace(CODES.begin(),CODES.end(),'U','5');
        replace(CODES.begin(),CODES.end(),'T','6');
        replace(CODES.begin(),CODES.end(),'E','7');
        replace(CODES.begin(),CODES.end(),'R','8');
        replace(CODES.begin(),CODES.end(),'S','9');
        replace(CODES.begin(),CODES.end(),'X','0');
        replace(CODES.begin(),CODES.end(),'c','1');
        replace(CODES.begin(),CODES.end(),'o','2');
        replace(CODES.begin(),CODES.end(),'m','3');
        replace(CODES.begin(),CODES.end(),'p','4');
        replace(CODES.begin(),CODES.end(),'u','5');
        replace(CODES.begin(),CODES.end(),'t','6');
        replace(CODES.begin(),CODES.end(),'e','7');
        replace(CODES.begin(),CODES.end(),'r','8');
        replace(CODES.begin(),CODES.end(),'s','9');
        replace(CODES.begin(),CODES.end(),'x','0');

        cout << "Value: " << CODES << endl;

        cout << "Do you want to enter another code? (Y/N) ";
        cin >> choice;
    }
    while(strcmpi(choice, 'yes') == 0 ||strcmpi(choice, 'y') == 0 );
    {
        if (strcmpi(choice, 'no') == 0 || strcpmi(choice, 'no' == 0);
        {
        cout << "Program terminate";
        }
    }

    return 0;
}

any help will be appreciated, thank you very much

ps. i prefer classic c

CodePudding user response:

Try something like this:

#include <iostream>
#include <algorithm>

const std::string allowedChars = "COMPUTERS.X";
const std::pair<char, char> translations[] = {{'C', '1'}, {'O', '2'}, {'M', '3'}, {'P', '4'}, {'U', '5'}, 
                                         {'T', '6'}, {'E', '7'}, {'R', '8'}, {'S', '9'}, {'.', '.'}, {'X', 'X'}};

bool stringIsCorrect(std::string s) {
    for (const auto& c : s) {
        if (allowedChars.find(c) == std::string::npos) {
            return false;
        }
    }
    return true;
}

char findTranslation(char c) {
    for(const auto& [from, to] : translations) {
        if(c == from) {
            return to;
        }
    }
    return -1;
}

int main() {
    std::string str = "coMPuteRS.x"; // or input it.
    // first we make it uppercase
    std::transform(str.begin(), str.end(), str.begin(), ::toupper);
    // we check 's correct
    if (!stringIsCorrect(str)) {
        return 1; // or ask again...
    }
    // finally we translate 
    std::transform(str.begin(), str.end(), str.begin(), findTranslation);
    std::cout << str << std::endl;
    return 0;
}

I'm not sure if I understood it correctly, since all letters from 'most' are included in string 'computers' so tell me if I'm wrong.

CodePudding user response:

Use a array to save convert rules. And both convert to upper to ignore case.

#include <algorithm>
#include <cstring>
#include <iostream>

using namespace std;

bool encode(string& old, const char* replace_rules[]) {
  int i = 0;
  int replaced = 0;

  while (replace_rules[0][i] != '\0') {
    for (int j = 0; j < old.size(); j  ) {
      // convert to upper case
      if (old[j] >= 'a' && old[j] <= 'z') {
        old[j] = old[j] - 'a'   'A';
      }

      if (old[j] == replace_rules[0][i]) {
        old[j] = replace_rules[1][i];
        replaced  ;
      }
    }
    i  ;
  }
  if (replaced != old.size()) {
    cout << "UNABLE TO CONVERT YOUR INPUT" << endl;
    return false;
  }
  return true;
}

int main() {
  string codes;
  string choice;

  const char* replace_rules[2] = {"COMPUTERSX", "1234567890"};

  do {
    cout << "Input code:  ";
    cin >> codes;
    if (encode(codes, replace_rules)) {
      cout << "Value: " << codes << endl;
    }
    cout << "Do you want to enter another code? (Y/N) ";
    cin >> choice;
  } while (choice == "Y" || choice == "y");

  cout << "Program terminate";

  return 0;
}

Result:

Input code:  comPutErSspxX
Value: 1234567899400
  •  Tags:  
  • c
  • Related