skip to Main Content

The purpose of this code is to insert an x in between repeating letters. For example, if I were to input "CoolBoolFallmoose", the output would be "CoxolBoxolFalxlmoxose".

The code is also supposed to make an even number of pairs of letters, so if there is an odd amount of characters, an x is added to the end of the string. An example for this would be if we had "ball", it would become "balxlx" to make even pairs: "ba" "lx" "lx".

This is the code I have so far:

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

int main(){
    string cipher, plain, paired = "";
    cout << "input plaintext(no spaces, lowercase):n";
    cin >> plain;

    for (int i=0;i<plain.length();i++){
        if (plain[i]==plain[i+1]){
            plain.insert(i,'x');
        }
        paired[i]=paired[i];
        cout<<paired[i];
    }
   
    if (paired.length() % 2!= 0){
        paired=+'x';
    }
    
    cout<<paired<<endl;
    return 0;
}

The output I get is just the same as my input, no "x" added in any place.

The issue I am having is, every time I try to use the append() or insert() function for strings, I get an error from my compiler, which is xCode. Is there another way to solve this code?

EDIT: The error says:

No matching member function to call for insert

It also comes up for append().

3

Answers


  1. I don’t really know what you wanted to do with this part:

    paired[i]=paired[i];
    cout<<paired[i];
    

    but otherwise the logic is good. Here is my take on it, x is a counter:

    #include <iostream>
    #include <string>
    using namespace std;
    
    int main(){
        string m,n;
        int x = 0;
        
        cout << "Input: " << endl;
        getline(cin, m);
        
        for(int i = 0;i < m.length();i++){
            x++;
            n = n + m[i];
            if(m[i] == m[i+1]){
                n = n + 'x';
                x++;
            }
        }
        if((x % 2) != 0){
            n = n + 'x';
        }
        cout << n;
    
        return 0;
    }
    
    Login or Signup to reply.
  2. If you look at the available overloads of std::string::insert(), you will see that your statement plain.insert(i,'x'); does not match any of them, hence the compiler error. The overloads that takes a single char require either:

    • an index and a count (you are omitting the count)

    • an iterator and an optional count

    There is, however, a couple of overloads that take just an index and a value, but they require a const char* or a std::string, not a single char.

    Also, paired[i]=paired[i]; is a no-op. Except in your case, since paired has a size() of 0 since you never append anything to paired, so actually any access to paired[...] is undefined behavior.

    Try this instead:

    #include <iostream>
    #include <string>
    using namespace std;
    
    int main(){
        string plain, paired;
        cout << "input plaintext(no spaces, lowercase):n";
        cin >> plain;
    
        paired = plain;
        for (string::size_type i = 1; i < paired.size(); ++i){
            if (paired[i] == paired[i-1]){
                paired.insert(i, 1, 'x');
                // or: paired.insert(paired.begin()+i, 'x');
                // or: paired.insert(i, "x");
                // or: paired.insert(i, string{'x'});
                // or: paired.insert(paired.begin()+i, {'x'});
    
                ++i; // skip the x just inserted
            }
        }
       
        if (paired.size() % 2 != 0){
            paired += 'x';
        }
        
        cout << paired << endl;
        return 0;
    }
    

    Demo

    Login or Signup to reply.
  3. A couple of points

    First, Although the string.insert function says it takes an int as its first argument it really wants an iterator in this case.

    Second, you are inserting elements into your "plain" string which increases its length and you have plain.length within your loop so you create an infinite loop.

    Third, insert inserts BEFORE the index so you need to add 1 to I.

    The code below will work for your loop:

    Int len = plain.length();
    
    Int count = 0;
    
    for (int i = 0;  i < len + count; i++)
       {
           If (plain[i] == plain[i + 1])
              {
                 plain.insert(plain.begin() + (i +1), 'X');
                 ++count;
              }
       }
    
    cout << plain;
    

    And as, mentioned below, if you want to handle spaces you can use getline(cin, plain) instead of cin.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search