Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 266
Programmer Avatar asked May 25 '20 16:05

Programmer


1 Answers

When you need a character array you do not have to use individual character literals one by one, as in

char albet[] = {'a','b','c','d','e','f',... uff this is tedious ...};

You can use a string literal instead:

const std::string albet{"abcdefghijklmnopqrstuvwxyz"};

Took me ~10 seconds to type and compared to other answers, this does not rely on ASCII encoding (which is not guaranteed).

like image 178
463035818_is_not_a_number Avatar answered Sep 28 '22 02:09

463035818_is_not_a_number