Link to home
Start Free TrialLog in
Avatar of plinius
plinius

asked on

most efficient method to do this

Hi,
I made a program to make all possible "words"  (from a to zzzzz for example) which uses this function:
***
char increaseChar (char value)
{        
  if  (value != ' z' ) return ( char (value + 1) );
  else return 'a';
}
***
Now I want to adapt the program so that you can define your own alphabet (for example "azerty123&é" )

What is the best way to do this??
How should I adapt the increasechar function to do this??
Or is there a better way ??
(like using an "enum" , or something like this ? )

Could you also give a code sample please, since I am a beginner.
Avatar of rstaveley
rstaveley
Flag of United Kingdom of Great Britain and Northern Ireland image

You could do a look-up for all characters. Simply index the array using the character value.

The following maps characters 'A'-'Z' and 'a'-'z':
--------8<--------
#include <iostream>
#include <string>
#include <algorithm>

char togibber(char c)
{
      static const char upper[] = "QWERTYUIOPASDFGHJKLZXCVBNM";
      static const char lower[] = "qwertyuiopasdfghjklzxcvbnm";

      if (c >= 'A' && c <= 'Z')
            return upper[c-'A'];

      if (c >= 'a' && c <= 'z')
            return lower[c-'a'];

      return c;
}

int main()
{
      std::string str = "Hello, confused world";
      transform(str.begin(),str.end(),str.begin(),togibber);
      std::cout << str << '\n';
}
--------8<--------
SOLUTION
Avatar of efn
efn

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of plinius
plinius

ASKER

Thanks all for your answers.
rstaveley, this wasn't exactly what I ment, but thanks anyway.
I used anthony_w's example.