Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 233
  • Last Modified:

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.
0
plinius
Asked:
plinius
2 Solutions
 
rstaveleyCommented:
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<--------
0
 
efnCommented:
Put the alphabet in an array and instead of using letters themselves, use numbers that index the array.  Then instead of calling an increaseChar function, you can just add 1 to the number.  Use the numbers as array subscripts to get the letters for output.

You could use a string object instead of an array if you want to be more C++y.

Sketchy example:

const char* alphabet = "blah";
int alphabetLength = strlen(alphabet);

// To generate all the two-letter words from this alphabet:

char word[3];
word[2] = '\0';
for (int sub1 = 0; sub1 < alphabetLength; ++sub1)
    for (int sub2 = 0; sub2 < alphabetLength; ++sub2)
    {
        word[0] = alphabet[sub1];
        word[1] = alphabet[sub2];
        //  Save or output word somehow here.
    }

(Clearly I had an interpretation of the question completely different from that of my esteemed colleague rstaveley.)

--efn
0
 
anthony_wCommented:
If you have your own alphabet, then you can rewrite increaseChar as below to return the next char, given a character from the alphabet, or the same character if given a character not there. You just then need to make your main loop deal with the custom alphabet.

char const alphabet[]="azerty123&é";

char increaseChar(char start)
{
    static const int charsInAlphabet=strlen(alphabet);
    int const index=std::find(alphabet,alphabet+charsInAlphabet,start);
    if(!alphabet[index]) // character not found
    {
        return start;
    }
    else
    {
        int const nextIndex=(index+1)%charsInAlphabet;
        return alphabet[nextIndex];
    }
}
0
 
pliniusAuthor Commented:
Thanks all for your answers.
rstaveley, this wasn't exactly what I ment, but thanks anyway.
I used anthony_w's example.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now