Hi, for some reason my code is not couting right. My function is supposed to decipher some code that if it has multiple same chars then it drops one. Example aabbyfkk --------> abyfk. But it couts abyffkk . For some reason it is not getting rid of the extra f and k chars.
string decrypt (string encrypted)
{
string deleted, tmp;
int i, pos, n, j=0, z;
tmp=encrypted;
/*--------First Step: The for loop goes through the elements first element and following elements next to it, if they are not the same then the first elements is read and copied into the string tmp---------*/
for (i=0; i < (encrypted.size() - 1); i++)
{
I think your problems arise from your use of tmp = encrypted. When you're decrypting, you're overwriting a portion of that string, but some of the original encrypted string will remain at the end.
Now, in terms of assuring that your tmp string will never be too small, this isn't a bad strategy (albeit you could have just called the resize member function and passed the size of the encrypted string as an argument).
Just make sure to shrink your tmp string once you know how long the decrypted string is. :)
NOTE: Depending on your approach, you may also find yourself experiencing problems with the last character of the encrypted string being chopped of. I leave that problem for you to solve.
Yeah it seems like it is from tmp= encrypted, but Im not sure where to resize the tmp and by how much should I resize it. Can you help me? I am new to C++ and I am trying my best on this.