Search code examples
c++stringloopstextalter

Compare two strings add(empty space, commas, dots, etc) c++


Hi there all I hope someone can help me out, I am trying to fix my string output, I get an output but its all mashed up into one long string no spaces etc, so I am trying to use the old string that was there before with commas spaces etc and replace them in the new one. I don't really know how to go about doing this I have tried this in c++ if I run the code below in my program it will crash on me I have no idea how to fix it.

string input2 = "chq pste uwdn or mfwiejjne
u aaul mvabe mrv po zso, jnp kg co urfrsrvwn,
azh s bmmpd lanmf kuupv chqvw, xf opsh azh ojtfpwb mmhw;
wizi tnaz vgfs imdu i tenn ttijn, a tmnn fav lqe tsfny niw,
jnp paee mpgwe ur lqe niw uogh yuapi.
swd u wzjlx lsee esen pqeun ttijn, fav hnaoi uxmqw vaobtawg epgf,
ddshyizk xaoy xzn vqmdb or xzn mavfrns xg fhqvw chq gjrcwil bizkk;
chqvw vipraphf’w sul m kdrmyij, jnp rgxn m tmapxi yuoi,
efm ehifrns jmul aj lqe xmfwef’w ornsw.
a fixp saiei swd ss fxw, rsj jlieqb nukzc azh vjy
u lwjr xecn wmxwa lmthrns aach xso bogrvb bk xzn stsjn;
wtmdn i exswd ar lqe dssmwmc, ga oz xzn pmzwvezxk prqc,
a qemv ac iz xzn dqih qemvl’b cavw.";


string result = "THELAKEISLEOFINNISFREEIWILLARISEANDGONOWANDGOTOINNISFREEANDASMALLCAB
INBUILDTHEREOFCLAYANDWATTLESMADENINEBEANROWSWILLIHAVETHEREAHIVEFORTH
EHONEYBEEANDLIVEALONEINTHEBEELOUDGLADEANDISHALLHAVESOME
PEACETHEREFORPEACECOMESDROPPINGSLOWDROPPINGFROMTHEVEILSOFTHEMORNINGTOWHERETHE
CRICKETSINGSTHEREMIDNIGHTSALLAGLIMMERANDNOONAPURPLEGLOWANDEVENINGFULLOFTHELINNET
SWINGSIWILLARISEANDGONOWFORALWAYSNIGHTANDDAYIHEARLAKEWATERLAPPINGWITHLOWSOUNDSB
YTHESHOREWHILEISTANDONTHEROADWAYORONTHEPAVEMENTSGREYIHEARITINTHEDEEPHEARTSCORE";

string original = input2; //original unchanged text with spaces, commas etc
string tempResult = result; //the new output without spaces, commas, dots etc
string fixed = ""; //string to be altered with spaces, commas etc
int originalSize = input2.size();
int i = 0, j = 0;

for(i, j; i < originalSize ;i++)
{
  if(isalnum(original[i]) || !original[i] == ' ')
{
  fixed += tempResult[j];
  j++;
}
else
{
  fixed += tempResult[j];
}
}

Solution

  • As noted in the comments, providing a Minimal, Complete and Verifiable example would have made it easier to understand your question, but I think I understand what you're trying to do. Making minimal modifications to your code, one solution might be this:

    #include <iostream>
    #include <string>
    
    using namespace std;
    
    string original = "xxxx, x xxxxx, xx x xxxxxx xxxxxxxx.";
    string letters = "ThisIthinkisabetterquestion";
    string desired = "This, I think, is a better question.";
    
    int main()
    {
        string fixed;
        int i, j;
        int originalSize = original.size();
    
        for(i=j=0; i < originalSize ;i++)
        {
            if(!isalnum(original[i]))
            {
              fixed += original[i];
            }
            else
            {
              fixed += letters[j++];
            }
        }
        cout << fixed << endl;
        cout << desired << endl;
        return 0;
    }