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

arrays-如何获得一种更简单的方法来生成C ++中的字母?

(arrays - How to get an easier way of generating the alphabet in C++?)

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

我正在尝试制作一个项目,以试验和学习C ++,但我没有完成它,但是它的作用是你输入3或4(变量noc)字,并且程序运行所有可能的(noc)字母单词或废话,直到找到你的单词,所以有两个因素:单词或废话的长度以及可以键入的字符,在我的情况下,我只想要字母,所以这是我的代码:

#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;
}

目前,我需要数组中的字母“ albet”(我想出一些简单的单词来表示它们容易忘记),所以请你能给我一种快速用C ++生成字母的方法,而不必键入全部他们一个接一个

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

因为所有字符都可以用ASCII码表示(“ a”开头为97,所以所有ASCII码都为int),因此你可以简单地循环执行该操作。例如:

char albet[26];

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

你就完成了!