Velocity Reviews > C++ > Character shifting

# Character shifting

Bluegrass
Guest
Posts: n/a

 04-02-2010
I have an interesting little algorithm that I'm getting myself wrapped
up in, to the point I'm locked into a pattern of thinking so I thought
I'd see if there were some alternate viewpoints

I'd like to take a word, lets say "Gotcha" and then shift letters based
on a key, an array of letters, ... but stay within that array.

for example, the Gotcha and an array of vowels, 'A', 'E', 'I', 'O', 'U'

so I'm going to mod the lenth of the word with sizeof array and get the
shift pattern.

so 6 % 5

How's a good way to loop back into the beginning of the array, so the
mod of a number gives me a result that would take it past the 'U' I'd
like to circle back to the beginning.

So first garble the word, then be able to reverse the process to
un-garble the word.

Thanks

Bluegrass
Guest
Posts: n/a

 04-03-2010
On 2010-04-02 23:29:24 -0400, Victor Bazarov <(E-Mail Removed)> said:

> Bluegrass wrote:
>> I have an interesting little algorithm that I'm getting myself wrapped
>> up in, to the point I'm locked into a pattern of thinking so I thought
>> I'd see if there were some alternate viewpoints
>>
>> I'd like to take a word, lets say "Gotcha" and then shift letters based
>> on a key, an array of letters, ... but stay within that array.
>>
>> for example, the Gotcha and an array of vowels, 'A', 'E', 'I', 'O', 'U'
>>
>> so I'm going to mod the lenth of the word with sizeof array and get the
>> shift pattern.
>>
>> so 6 % 5
>>
>> How's a good way to loop back into the beginning of the array, so the
>> mod of a number gives me a result that would take it past the 'U' I'd
>> like to circle back to the beginning.
>>
>> So first garble the word, then be able to reverse the process to
>> un-garble the word.

>
> Wrong newsgroup. Try 'comp.programming'. If/when you get a C++
> language question, do come back. We specialize in those. Best of luck!
>
> V

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.

red floyd
Guest
Posts: n/a

 04-04-2010
On 4/3/2010 12:10 PM, Bluegrass wrote:
> On 2010-04-02 23:29:24 -0400, Victor Bazarov <(E-Mail Removed)>
> said:
>
>> Bluegrass wrote:
>>> [problem statement redacted]
>>>

>> Wrong newsgroup. Try 'comp.programming'. If/when you get a C++
>> language question, do come back. We specialize in those. Best of luck!

>
> 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.

Doesn't matter. We aren't going to write it for you. Show us what
you've done, and we'll critique and/or make suggestion.

Bluegrass
Guest
Posts: n/a

 04-04-2010
On 2010-04-04 07:43:48 -0400, Christian Hackl <(E-Mail Removed)> 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.

Kai-Uwe Bux
Guest
Posts: n/a

 04-04-2010
Bluegrass wrote:

> On 2010-04-04 07:43:48 -0400, Christian Hackl <(E-Mail Removed)> 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

Bluegrass
Guest
Posts: n/a

 04-06-2010
On 2010-04-04 16:13:21 -0400, Kai-Uwe Bux <(E-Mail Removed)> said:

> Bluegrass wrote:
>
>> On 2010-04-04 07:43:48 -0400, Christian Hackl <(E-Mail Removed)> 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...

Kai-Uwe Bux
Guest
Posts: n/a

 04-06-2010
Bluegrass wrote:

> On 2010-04-04 16:13:21 -0400, Kai-Uwe Bux <(E-Mail Removed)> 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