0

I was writing a code that would substitute some random 17 character strings into a single alphabet, and I can't find a way. Basically, what I'm trying to do is this:

char strings[] = {
    "L-nIbhm5<z:92~+,x",
    "9bC5f0q@qA(RKZ>|r",
    "9bC5f0q@qA(RKZ>|r",
    "k=5,ln(08IAl(gGAK",
    "|N,8]dGu)'^MaYpu[",
    "!&,Y*nz8C*,J}{+d]",
    "Us9%^%?n5!~e@@*+@",
    "zF8,1KV#¥]$k?|9R#",
    "0B4>=nioEjp>4rhgi",
}

char alphabet[]{
    "a","b","c","d","e","f","g","h","i",
}

replace(std::string str){
    /**get str and then see the index of the corresponding string in strings[], and replace the string with alphabet[index number], while deleting the original string part that was replaced**/

int main(){
    cin >> std::string replace;
    replace(replace);

example input: L-nIbhm5<z:92~+,x9bC5f0q@qA(RKZ>|r9bC5f0q@qA(RKZ>|r

expected output: abc

EDIT: New Code Changes from the original code

It also has a bigger array than the simplified version(previous code). It displays the structure of the full program.(where the strings are routed to and why)

Basically What it's doing

getting input from user, put it in the input variable, input goes through algorithm() function untouched, and then goes to the replace function and is replaced. It then the replaced string gets returned back through the original route to the main function, where it is displayed.

I've kept the arrays a string type because the const char* gave me a segmentation error.

std::string Subs[53]=
{
    "LQlMv]G5^^1kcm?fk",
    "7W^S;/vB(6%I|w[fl",
    "<w7>4f//Z55ZxK'z.",
    "_W5g(lu<pTu3^_A7n",
    "OfLm%8:EF}0V1?BSS",
    "|+E6t,AZ~XewXP17T",
    "L-nIbhm5<z:92~+,x",
    "L-nIbhm5<z:92~+,x",
    "9bC5f0q@qA(RKZ>|r",
    "9bC5f0q@qA(RKZ>|r",
    "k=5,ln(08IAl(gGAK",
    "|N,8]dGu)'^MaYpu[",
    "!&,Y*nz8C*,J}{+d]",
    "Us9%^%?n5!~e@@*+@",
    "zF8,1KV#¥]$k?|9R#",
    "0B4>=nioEjp>4rhgi",
    "EG@0[W9.N4i~E<f3x",
    "(0Pwkk&IPchJHs.7A",
    "7XgmQ6fW<|J+NY[m0",
    ".g4CwX/DU!!~!zbtZ",
    "+_U'qn_/9Fo|gT/!n",
    "=0s(mYh&F%y=MBS5(",
    "cg71(}bo+Q5P8F[T6",
    "lc|a\%5.9pOpooU+QR",
    "E_(3A:o+.]qL3MYA6",
    "H@O'X_RiVS@8l0bKD",
    "Y1gbGD`~8d>HSWN35",
    "LQlMv]G5^^1kcm?fk",
    "T4}gI;`BFVfhw=-sf",
    "6BHMA0IRix]/=(jht",
    "yS$=@Jdpp?P2k6SMQ",
    "t1~|kkh+>4d>}OQ`a",
    "2Y-\\CU\"944yBluWD5",
    "'M\\ZbIX5{`Xd;qi!o",
    "?N+RtVqj_r(C5@#0\"",
    "2;*Livh?V$X/8z@Md",
    ")IN|7FOs2l-mAM[d@",
    "(~f268J},xXrK'Rp'",
    "&r/qf9fFHnzV!RzH/",
    "}naDRH4p$NI2a).t,",
    "{8DM+7!.Mge|~fnO|",
    ")r[@nI0YDH>6cE38p",
    "(0Pwkk&IPchJHs.7A",
    ")r[@nI0YDH>6cE38p",
    "8M-=cQFQ,pPo7eu=p",
    "0PHw=/|(tZ1}FHm/'",
    "[su`'0Oybc.\"-/W5)",
    "1uHl[IC7Sr#NUJV;I",
    "8z8%,jK0CDOkJz8I?",
    "3Ao2yXDN%YzpE&Suy",
    "zNs`7E'e/$i8VqaUL",
    "bzHmA^K2>7`UZ?!AO",
};

std::string Alphabet[53] = 
{
    " ","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","r","w","x","y","z",
    "A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z",
};


std::string replace(std::string rep) {
    int len = sizeof(Subs)/sizeof(Subs[0]);
    std::stringstream ss1;
    for(int i = 0; i < len; i++) {
        if (rep.find(Subs[i]) != std::string::npos) {
            ss1 << Subs[i];
        }
    }
    std::string input = ss1.str();
    return input;
}

std::string algorithm(std::string input)
{
    //some other algorithms come here(not relative to this question)
    input = replace(input);
    return input;
}

int main(void){
    int ed;
    std::cin >> ed;

    if(ed == 1){
//different function(not relative to the question)
        }
    else if(ed == 0){
        std::string input;
        std::cin >> input;
        input = algorithm(input);
        std::cout << input << std::endl;
    }

    else{
        std::cout << "1 or 0" << std::endl;
        main();
    }

return 0;
}

example input: L-nIbhm5<z:92~+,x9bC5f0q@qA(RKZ>|r9bC5f0q@qA(RKZ>|r

expected output: abc

actual output: L-nIbhm5<z:92~+,xL-nIbhm5<z:92~+,x9bC5f0q@qA(RKZ>|r9bC5f0q@qA(RKZ>|r

Sorry it's become long.

Hal Hana
  • 25
  • 5

2 Answers2

1

There are few mistakes in above code :

  • char array initialization is not correct.
  • method body for main and replace method is not closed.
  • Currently by default return type of replace method is int.

There is string#find method which can be helpful here.

I have tried to make those fixes and here is updated code in C++17 :

#include <iostream>
#include <sstream>
using namespace std;

const char *strings[9] = {
    "L-nIbhm5<z:92~+,x",
    "9bC5f0q@qA(RKZ>|r",
    "9bC5f0q@qA(RKZ>|r",
    "k=5,ln(08IAl(gGAK",
    "|N,8]dGu)'^MaYpu[",
    "!&,Y*nz8C*,J}{+d]",
    "Us9%^%?n5!~e@@*+@",
    "zF8,1KV#¥]$k?|9R#",
    "0B4>=nioEjp>4rhgi"
};

const char *alphabet[9] = {
    "a","b","c","d","e","f","g","h","i"
};

void replace(std::string rep) {
    int len = sizeof(strings)/sizeof(strings[0]);
    std::stringstream ss1;
    for(int i = 0; i < len; i++) {
        if (rep.find(strings[i]) != std::string::npos) {
            ss1 << alphabet[i];
        }
    }
    std::cout << ss1.str();
}

int main(){
    std::string rep;
    cin >> rep;
    replace(rep);
}

For reference : https://onlinegdb.com/Bd9DXSPAa

Note - Above code is just for reference, please make sure to add all test cases handling.

SRJ
  • 2,092
  • 3
  • 17
  • 36
  • 1
    [Why should I not #include ?](https://stackoverflow.com/questions/31816095/) – Remy Lebeau Sep 08 '21 at 05:23
  • okay just to try quickly i added that, now we can replace it with `#include `. Updated the code. – SRJ Sep 08 '21 at 05:53
  • the code runs when it's standalone exactly the way you wrote it, but when I try to add more elements into the array(like the whole alphabet worth, it doesn't work(not an error, it just comes out as fghiZ) – Hal Hana Sep 08 '21 at 06:16
  • Can you add that example in question with sample input and output ? – SRJ Sep 08 '21 at 06:18
  • i've edited the question so that it outlines the complete image of the whole program. i've also added the actual input and output and expected output. it has a different output as my previous comment, which is because my previous comment was a standalone, and the edited answer is a whole. I wanted to put the standalone program into the edit as well, but figured the edit would become too long. – Hal Hana Sep 08 '21 at 08:23
0

I made a c++17 version for your code. Replacing 'c' style arrays and pointers with C++ style containers, iterators. And using std::string::replace function. Use the standardlibrary if you can, its tested and well documented.

#include <algorithm>
#include <iostream>
#include <regex>
#include <string>
#include <vector>

// std::vector/std::array instead of 'c' style arrays.
// allows us to us range based for loops later.
std::vector<std::string> strings =
{
        "L-nIbhm5<z:92~+,x",
        "9bC5f0q@qA(RKZ>|r",
        "k=5,ln(08IAl(gGAK",
        "|N,8]dGu)'^MaYpu[",
        "!&,Y*nz8C*,J}{+d]",
        "Us9%^%?n5!~e@@*+@",
        //"zF8,1KV#¥]$k?|9R#", // <<== I commented out this line, ¥ is not a valid charcter in my environment
        "0B4>=nioEjp>4rhgi"
};

// a string is already an array of characters.
std::string alphabet{ "abcdefghijkl" };

std::string replace_with_alphabet(const std::string& input)
{
    std::string retval{ input };
    std::size_t index{ 0 };

    // range based for, it will keep the order of the vector.
    for (const auto& str : strings)
    {
        // look if you can find any of the predefined strings 
        // in the input strings.
        const size_t pos = retval.find(str, 0);

        // if found
        if (pos != std::string::npos) 
        {
            // get the next character from the alphabet
            std::string replacement{ alphabet[index++] };

            // use std::string::replace for replacing the substring
            const size_t len = str.length();
            retval.replace(pos, len, replacement, 0);
        }
    }

    return retval;
};

/**get str and then see the index of the corresponding string in strings[], and replace the string with alphabet[index number], while deleting the original string part that was replaced**/
int main()
{
    auto output = replace_with_alphabet("L-nIbhm5<z:92~+,x9bC5f0q@qA(RKZ>|rk=5,ln(08IAl(gGAK");
    std::cout << output << std::endl;
}
Pepijn Kramer
  • 9,356
  • 2
  • 8
  • 19