most efficient method to do this

Posted on 2004-11-09
Last Modified: 2012-05-05
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.
Question by:plinius
    LVL 17

    Expert Comment

    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':
    #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";
          std::cout << str << '\n';
    LVL 15

    Assisted Solution

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

    LVL 4

    Accepted Solution

    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;
            int const nextIndex=(index+1)%charsInAlphabet;
            return alphabet[nextIndex];

    Author Comment

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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
    C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (…
    The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
    The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now