I have this problem and I have others like this but I need to find a program that shows how to do this:
I need a program able to decrypt an encrypted string
-Words of the string are not ecrypted at all, exaple: Dollar "Do(rall)"
-If I type "Do(rall)" the program has to print "Dollar"
- If I type "W(neirf(est)b era (e))ds" the program has to printf "We are best friends"
-As you could see every time I use parentheses the words inside it are in backward
The program will notice that the encryption is inside parentheses and will decrypt it taking and ordering them out
So it is not like a decrypting program it is like a program that organizes the words inside parentheses
Is there any program able to do this?
I just know how to use C and only the stdio.h library and this one has to be in C++ so you are my hope
Sorry about my english I'm still learning this language.
ok. took me a bit time. but i wrote a programm that does waht you want.
lets start with the idea:
1) search for an unnested pair of (, ).
why unnested? because you can work on that part without worrying that you mess another pair of braces up.
2) when you have the position of the substring (txet) you can replace it with text.
goto 1 until ther are no bracepairs.
now here is what the programm (see below) will do to W(neirf (est)b era (e))ds:
W(neirf (est)b era (e))ds -> W(neirf (est)b era e)ds -> W(neirf tseb era e)ds -> We are best friends
and finaly the code, i have put some comments in that will explain what i did there, i hope ;)
#include <iostream>
#include <string>
usingnamespace std;
string func(string s); //this will decrypt your string
string reverse(string s); //this will just reverse a given string
int main(int argc, char **argv)
{
string input = argv[1]; //convert the given argument to a string.
cout << func(input) << endl;
return 0;
}
string func(string s)
{
//search the last (
size_t pos_open = s.find_last_of('(');
//if none was found there is nothing to decrypt
if (pos_open == string::npos) {
return s;
}
//search the first ) from the last (
size_t pos_close = s.find_first_of(')', pos_open);
//if none was found there is nothing to decrypt
if (pos_close == string::npos) {
return s;
}
//sub will contain the (text).
//e.g. (tse)
string sub = s.substr(pos_open + 1, (pos_close - pos_open - 1 ));
//replace (tse) with tse
s.replace(pos_open, (pos_close - pos_open + 1), reverse(x));
return func(s);
}
string reverse(const string &s)
{
string tmp;
for (size_t i = 0; i < s.size(); i++) {
tmp += s.at(s.size() - i - 1);
}
return tmp;
}
if you dont understand anything look at the string reference on this page or ask.
#include <iostream>
#include <string>
#include <algorithm>
usingnamespace std;
struct SwapLPsAndRPs
{
voidoperator()(char & ch)
{
if (ch=='(') ch=')';
elseif (ch==')') ch='(';
}
};
int main()
{
string str;
string::iterator left,right;
int lpos, rpos;
int par_level;
getline(cin,str);
while (true)
{
lpos=str.find_first_of('(');
if (lpos==string::npos) break;
par_level=1;
for (rpos=lpos+1; rpos<str.size(); rpos++)
{
if (str[rpos]==')') par_level--;
elseif (str[rpos]=='(') par_level++;
if (par_level==0) break;
}
if (rpos==str.size()) break;
left=str.begin()+lpos;
right=str.begin()+rpos;
reverse(left,right);
for_each(left,right-1,SwapLPsAndRPs());
str.erase(right-1,right+1);
}
cout << str << endl;
cout << "hit enter to quit..." << endl;
cin.get();
return 0;
}
At first I used find_last_of to get the right peranthesis position
but this was wrong so I ended up doing it in a for loop,
keeping track of the perenthesis nesting level.
quirkyusername: I already checked for programs like this on this forum
Mathes: I so happy with you answer you were so kind unfortunately on C++ compilation this program has this problem:
In function `st::string func(std::string)`:
on line 29 `x` undeclared (first use this function) but don't worry I'll use this program to learn
m4ster r0shi: Thank you very very much I'll check every single line of this program to learn how to resolve others problems like this your program is 100% working, I'm very grateful with your help and Mathes' too