Re: Character shifting
On 2010-04-04 16:13:21 -0400, Kai-Uwe Bux <jkherciueh@gmx.net> said:
Bluegrass wrote:
On 2010-04-04 07:43:48 -0400, Christian Hackl <hacki@sbox.tugraz.at> said:
Bluegrass ha scritto:
Considering I failed to mention that I wanted to implement it in C++ in
a fast effecient method, nothing would lead you to think I was in the
right group. Brute force for something like this could be slow, I was
looking for C++ experts to help a bit with my garbling of works.
The problem is that the *design* of the algorithm is not related to
C++. The main factor in determining its efficiency will be its
concept, which would be the same if you used PHP, Visual Basic or C#,
not the implementation.
Now if your question is how to *implement* it in C++, e.g. which
container class to use for a particular step of the algorithm, then
comp.lang.c++ is the right place to ask, of course, because obviously
this question would *not* be the same in PHP, Visual Basic or C#.
Hmm, good point, I was doing it with a String.Find_First_of to locate
the first character matching the 'key array' and then looping through
the 'key' array to find the position of the located char, taking the
mod between the string length, and the key array length, to get the
shift position, then adding that to my loop index of the array,
checking the upper/lower bound and a string.replace to get the new char
into my string. very brute force C++ with the only class being the
string....
so that in essence is my design, and some pseudo code into how I'm
handling it... just looking for some different viewpoints.
I have to admit that from your description, I have no idea what the
algorithm is supposed to be doing. Could you post some code (or pseudo
code)? Could you walk us through an example? Also, if you have a solution,
we would understand the problem and then can propose different solutions.
Right now, I don't see which algorithmic problem you are trying to solve.
Best
Kai-Uwe Bux
Here's a go at it
const TCHAR Vowels[5] = {'A','E','I','O','U'};
TCHAR* GarbleString(const TCHAR *strInput)
{
????? TCHAR* sRetVal = new TCHAR[MAX_PATH];
?
????? String strTmp = strInput;
?
????? std::transform(strTmp.begin(), strTmp.end(),strTmp.begin(),
(int(*)(int)) std::toupper);
?
????? size_t szFound;
????? szFound = strTmp.find_first_of(Vowels);
?
????? while (szFound != string::npos)
????? {
??????????? strTmp[szFound] = ModShift(strTmp.length(),
strTmp.at(szFound), true);
??????????? szFound = strTmp.find_first_of(Vowels,szFound+1);
????? }
???? std::transform(strTmp.begin(), strTmp.end(),strTmp.begin(),
(int(*)(int)) std::tolower);
?
????? int len = strTmp.length() +1;
????? sRetVal = new TCHAR(len);
????? _tcscpy_s(sRetVal, len, strTmp.data());
?
????? return sRetVal;
}
wchar_t ModShift(int strLen, wchar_t chRep, bool blnEncrypt)
{
????? // blnEncrypt to true to encrypt, false to decrypt
?
????? int intModRes = 0;
????? int intVowAry = sizeof (Vowels)/sizeof(TCHAR);
?
????? if(blnEncrypt)
????? {
???????????
??????????// calculate the mod value for vowels
??????????intModRes = intVowAry % strLen;
?
??????????// Find the character in the array
??????????for(int i = 0; i < intVowAry; i++)
??????????{
?????????????if(?? Vowels[i] == chRep)
???????????? {
????????????????if ((i + intModRes > intVowAry) || (intModRes == intVowAry))
????????????????{
???????????????????int rem = intVowAry - intModRes;
???????????????????return Vowels[rem];
???????????????? }
???????????????? else
?????????????????{
?????????????????? // Shift to the position and replace
?????????????????? return Vowels[intModRes];
????????????????? }
????????????? }
?????????? }
}
}
Not fully debugged, so it's rough but basically shift the vowels, then
be able to reverse it. Doing it on an windows box and I know TCHAR's
are a macro...