Re: Character shifting
Bluegrass wrote:
On 2010-04-04 16:13:21 -0400, Kai-Uwe Bux <jkherciueh@gmx.net> said:
[...]
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];
This return value does not depend on i ...
}
else
{
// Shift to the position and replace
return Vowels[intModRes];
.... and neither does this.
Also, the comment is weird since nothing is replaced.
Hm...
}
}
}
}
}
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...
Hm, I am not using windows. Here is a more standard version:
#include <string>
#include <algorithm>
#include <cassert>
#include <cctype>
std::string vowels = "AEIOU";
char upper ( char c ) {
return ( std::toupper(c) );
}
char lower ( char c ) {
return ( std::tolower(c) );
}
char mod_shift ( std::string::size_type length, char ch ) {
std::string::size_type const n_vowels = vowels.length();
std::string::size_type const mod_value = n_vowels % length;
for ( std::string::size_type i = 0; i < n_vowels; ++ i ) {
if ( ch == vowels[i] ) {
if ( (i+mod_value >= n_vowels ) || mod_value == n_vowels ) {
return ( vowels[ n_vowels - mod_value ] );
} else {
return ( vowels[ mod_value ] );
}
}
}
assert( false ); // falling through without returning a char
}
std::string garble ( std::string str ) {
std::transform( str.begin(), str.end(), str.begin(), &upper );
for ( std::string::size_type szFound = str.find_first_of( vowels );
szFound < std::string::npos;
szFound = str.find_first_of( vowels, szFound+1 ) ) {
str[ szFound ] = mod_shift( str.length(), str[ szFound ] );
}
std::transform( str.begin(), str.end(), str.begin(), &lower );
return ( str );
}
#include <iostream>
#include <ostream>
int main ( void ) {
std::cout << garble( "abcdefghi" ) << "\n";
}
Now, it appears that just every vowel is replaced by "a".
I still don't understand the shuffling, shifting, garbling that you want to
see. Maybe, I made a mistake in the translation. But at least, now I can
tell, that your garbling algorithm is supposed to only change the vowels in
the input string and leave the rest alone. What should it do to the vowels.
E.g., what would the following strings become:
hello --> h_ll_
aloa --> _l__
aeiou --> _____
and why.?
Best
Kai-Uwe Bux