Warm tip: This article is reproduced from serverfault.com, please click

How to get an easier way of generating the alphabet in C++?

发布于 2020-05-25 16:07:24

i am trying to make a project,to experiment and learn C++, i didnt finish making it,but what it does is you type in a 3 or 4 (the variable noc) word and the program runs through all the possible (noc) letter words or nonsense, until it finds yours,so there are 2 factors: the length of the word or nonsense and what characters it can type,in my case i just want the alphabet so here is my code:

#include <iostream>
#include <unistd.h>
using namespace std;

const int noc = 3;

int main() {

    string used[noc];
    string inp;
    cin >> inp;
    char albet[] = {'a','b','c'};
    cout << "Starting..." << endl;
    usleep(1);
    string aiput = "";
    while(aiput != inp){
        for(int i = 0; i <= noc; i++){
            aiput = aiput +
        }
    }

    return 0;
}

currently i need the alphabet in the array called 'albet' (i come up with short words for what they mean its easy to forget tho) so please can you get me a way to generate the alphabet in C++ quickly instead of having to type all of them one by one

Questioner
Programmer
Viewed
0
David W 2020-05-26 00:15:37

Because all characters can be represented in ASCII codes ('a' starts at 97, all ASCII codes are int), you can simply make a loop to do that. For example:

char albet[26];

for (int ch = 'a'; ch <= 'z'; ch++) {
    //do ch-'a' because we start at index 0
    albet[ch-'a'] = ch;
}

and you are done!