Decryption code

Pages: 12
@stereoMatching
...what about things like
- events
- delegates
- interface
- good compiler error messages not only saying "i dont know but there is something wrong - dont ask me where..."^^
...not in c++.

and
- who wants to even think about "delete"?!
- ...and "linking" and all that stuff our grandfathers did last century?

no big deal u will say - and you are right... but this isnt the task of the developer nowadays - these are problems allready perfectly solved and automatically handeled by computers. you just dont have to solve them over and over again - its done - get over it!

use ure time learning math, invent new heuristics for problem solving and really intersting stuff and not waste it by solving technical problems allready perfectly solved decades ago.
Last edited on
My compiler always gives me the line where the error occurred, and I can always determine what the error is. That said, the compiler I use the most often is an unmodified version of g++, which is one of the most commonly used compilers in the open-source community...

As for the other "features", those look a lot to me like syntactic sugar. Not very sweet sugar, either.

The delete issue is easily solved with auto_ptrs.

Linking is not a feature of C++, and I bet even Microsoft's C# compiler has a linker someone in the toolchain. ;)

I'm starting to think you're intending to troll, cdoubleplus. It's a good idea to suggest an alternative once, but never a good idea to impose it, which it looks like you're trying to do.

-Albatross

yeah you re right - c++ is just great!!
Sarcasm. *sigh* I guess the trolls are moving in, huh? That's the seventh this year, and it's not even February yet.

I'm giving you a second calm warning. On the third, I'll report you to the forum admins. Before I do, though, I'd like to ask... why are you here? What's the purpose of storming into a C++ forum and touting C#'s excellence, when many of the people here have already made up their mind to go with C++? All you do is hurt the C# programmer community.

-Albatross
Alright, I am really new to C++, REALLY new. I didn't plan on making functions and stuff for my programs because, well, I don't know how to use them. I know how to do math functions like sine cosine tangent, basic math stuff, and I know how to manipulate strings to an extent. But beyond that I have no idea what maps or vectors are. I'm only on like chapter 3 on my Beginning C++ Through Game Programming book so I haven't got into intense topics. I really didn't expect this encyrption / decryption program to be that hard to write considering in Java all I had to do was have an if statement for every letter and use replaceAll() which did the looping for me. I'm trying to figure out the simplest way for me to do this. Maybe I should just read a little further ahead in my book and look at stuff on the site here?
If you used replaceAll() in Java, it might be a good idea to use the C++ "equivalent", which is std::replace.

std::replace(array,array+length, old_value, new_value)
See if the above works for you, and remember to #include <algorithm> !

-Albatross
But I'm using a string, not an array.
Strings? In that case, the code changes a little bit.

Strings support iterators, two of which you can access using the begin() and end() member functions. The following code should be... pretty intuitive. :)
std::replace(string.begin(),string.end(), old_value, new_value)

EDIT: Technically, the algorithm functions were meant to take iterators as arguments. However, pointers work just fine too, seeing as iterators give an interface that's a lot like arrays'/pointers'. I just thought I'd say that.

-Albatross
Last edited on
I had this as my test code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <algorithm>

using namespace std;

int main()
{

     string pdphrase;
     string pephrase;
     
     cout << "Enter the phrase you want encrypted: " << endl;
     cin >> pdphrase;
              
     replace(pdphrase.begin(), pdphrase.end(), "ab" , "ba");
          
     cout << pdphrase;

}


I get 2 errors: ISO C++ forbids comparison between pointer and integer and invalid conversion from 'const char*' to 'char'.
Wait a sec... so you want to encrypt little fragments of text instead of changing the values of individual characters?

Hmm. That could be a little bit tricker with that particular function. It would be a better move to use a loop, string.find() and string.replace(). Nevermind my earlier suggestion. :)

-Albatross
Last edited on
Just to make this clear (I didn't mean to sound rude but after rereading that i though it kind of was. I am not trying to be rude) I want the user to be able to type in something like a sentence. Then the program will go through and replace each letter with a capitilized letter and a number so if the sentence contained an a or multiple a's they would be replaced by Z2. That is what I'm trying to accomplish.
...oh! I see now!

I think we had a problem like that before. A general solution might be:

*Search for each character in order.
*Per search, use find(). You may want to declare a counter (call it counter1), set to zero at the start of your program.
*When you find a character, set your counter1 to its position, record the position in a different location (let's call this marker1), add one, and check what string[counter1] is. If it's the same character as the one you're looking for, add one to counter1 again. Repeat until the character differs from the one at marker1.
*replace() the found substring with a single capital letter (arguments: marker1, counter1 - marker1, character_to_replace), and if counter1 - 1 - marker1 isn't equal to zero, insert() an integer type-cast into a character.
*Continue the search from the position that counter1 indicates. In order words, repeat
*When starting a search for a new character, reset counter1.

I'm incredibly tired, but the above logic should work... best of luck, alright? :)

-Albatross
The character by character replacement could be done with a single array (the decryption always generating itself at runtime) using an implicit key.
This applies if the replacement is always the same.

Don't do it in-place, it will be simpler:
_ Loop trough the input.
_ Read the encryption that corresponds to that character.
_ Put the encryption in the output.

To decrypt (just an idea)
_ Generate a map of decryption (change the key, value of the encryption)
_ Loop
_ Keep extracting sub-strings until you found one on the map. (think on a way to optimise this)
_ Put the decoded value in the output
Sorry guys I have to go to bed and I was writing an English essay. I'll have to look over what you've said tomorrow after school. Thanks for the help though!
I think that I misread the problem.
Is this the expected output?
a -> Z1
aa -> Z2
aa... -> Zn

In that case, I will do the shrink first. a shrink function.
Last edited on
I get 2 errors: ISO C++ forbids comparison between pointer and integer and invalid conversion from 'const char*' to 'char'.

I have two solution to replace it
this is the solution I would like to use
boost::algorithm::replace_all(C, "T", "_PHANTOM_");
C is the string you want to manipulate
"T" would be replace by "_PHANTOM_"

this is the other solution
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  //input : the string you want to manipulate
 //ORIGIN : the string you want to replace
//REPLACE : ORIGIN would be replace by REPLACE
  void replaceALL(std::string& input, std::string const& ORIGIN, std::string const& REPLACE)
 {
    size_t j = input.find(ORIGIN);
    size_t const O_SIZE = ORIGIN.size();
    size_t const R_SIZE = REPLACE.size();
    while(j != std::string::npos)
   {
      input.replace(j, O_SIZE, REPLACE);
      j += R_SIZE;
      j = input.find(ORIGIN, j);
    }
  }


I would sugget you use the algorithm of boost directly(you could treat boost as a "sub-standard" lib)
there are a lot of instructions about how to install boost

ps : I am not sure the second solution is bug free, if there are any bugs or anyway could improve it
I would be very happy if anyone could tell me how to solve it
Thanks a lot
Last edited on
@ne555 No. I want a to turn into Z2, b to turn into B3. I have a list of randomized letter number combinations for each letter. I just want to have the user to enter the string and anytime a letter from the alphabet turns up it will get changed to its encrypted value. Its not really "encryption" but Its as close as I'm gonna get.
Assuming just lower-case characters.
1
2
3
4
5
std::string encoy_ring[] = {"Z2", "B3", "H4", "Z7", ...};
std::string result;
for(int K=0; K<input.size(); K++)
  result += encoy_ring[ input[K]-'a' ];
return result;


Now the decryption
1
2
3
std::map< std::string, char > decoy_ring;
for(char K='a'; K<='z'; K++) //swap key value
  decoy_ring[ encoy_ring[K-'a'] ] = K;
The problem consist in find a sub-strings that is inside the decoy ring. If you know something special about the values (like its size is 2) use it.
If not, something like this:
1
2
3
4
5
6
7
traverse the input{
  while( not present( decoy_ring, aux) ){
    aux = input.substr( beg, counter );
    counter++;
  }
  result += decoy_ring[ aux ];
}
Alright. I may just drop this project for now seeing as its way to difficult for me to be starting when I don't know that much about c++...
Topic archived. No new replies allowed.
Pages: 12