8

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

Programmer
  • 105
  • 1
  • 1
  • 5
  • 3
    `'a'`-`'z'` is contiguous in ASCII, but no guaranty in C++ (in EBCDIC, that range is not contiguous). I don't think you can generate it so with arithmetic in portable way. – Jarod42 May 25 '20 at 16:10
  • 1
    @Jarod42 Since OP is still learning there should be no problem relying on only ASCII. – 0x5453 May 25 '20 at 16:12
  • True, and the questioner may go through their entire programming career without seeing a non-contiguous or insanely ordered character set, but it's good to know they exist. – user4581301 May 25 '20 at 16:41
  • Why not just type in 26 letters as a string and use that string? English isn't a language that has thousands of characters. Sometimes the simplest solutions aren't so obvious. Even if you had to type all of them one-by-one, so what? That is 26 characters, 52, quotes, and commas. That is hardly any work, IMO. – PaulMcKenzie May 25 '20 at 16:59

6 Answers6

9

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

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
  • I upvoted this. My comments in the main section describe what you've done. It's amazing that sometimes the simplest solutions just get overlooked. – PaulMcKenzie May 25 '20 at 16:55
6

You could use std::iota, which is a great algorithm for this use case:

char albet[26] {};
std::iota(std::begin(albet), std::end(albet), 'a');

Here's a demo.

Note that this is not guaranteed to work in c++, unless you have ASCII encoding, but if you can rely on that you'll be fine.

cigien
  • 57,834
  • 11
  • 73
  • 112
  • I think it would be neat if the standard library had a compile-time constant to check if string literal encoding is ASCII, or at least if alphabet is consecutive. There is [already one for checking that floating point types us iec559](https://en.cppreference.com/w/cpp/types/numeric_limits/is_iec559) and it feels like the use case for that is narrower than checking that letters are contiguous. – François Andrieux May 25 '20 at 16:24
  • @FrançoisAndrieux That would be nice. I wonder what the applicability of this is, i.e. are there many architectures that use non-ascii encoding? If not, I expect eventually the standard to mandate this, like they've done for non 2'scomplement architectures. – cigien May 25 '20 at 16:28
5

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!

David W
  • 158
  • 1
  • 2
  • 9
1

Each letter has an ASCII representation. More about that here.

They are processed as numbers, being cast, and transformed into characters. For example, the letter a would be represented by the number 97 in decimal.

int aInAscii = 97;
printf("%c", (char)aInAscii);

The upper code would print, as you expect, the letter a. Why? Because we have just converted the number 97 to it's ASCII corresponding character.

So, in this way, we could generate the alphabet, using only numbers. A short example would be here (I preferred casting it before so that the starting and ending points are more clear.

#include <iostream>
#include <vector>

using namespace std;

int main() {
    vector<char> alphabet;
    int aLetterCode = (int)'a'; // ASCII code for letter a (97)
    int zLetterCode = (int)'z'; // ASCII code for letter z (122)
    for (int charAsciiCode = aLetterCode; charAsciiCode <= zLetterCode; charAsciiCode++) {
        alphabet.push_back((char)charAsciiCode);
    }
    for (char c : alphabet) {
        cout << c << " ";
    }
    return 0;
}
SealsRock12
  • 161
  • 2
  • 13
AlinP25
  • 95
  • 1
  • 11
1

You just can also make a function that returns a char, without generating an array, like this:

char get_alphabet_letter(unsigned int index, bool is_upper = false)
{
    char letter = 97 + index;
    if(is_upper) return letter - 32;
    return letter;  
}
Diego Velasco
  • 111
  • 1
  • 4
1

from the given below code, you can generate uppercase alphabets of English. Uppercase alphabhets starts in ASCII from A = 65 to Z = 90. And, then, typecast the integral value of uppercase alphabets into character using char().

#include <iostream>
using namespace std;

int main () {
    char a[26];
for (int i=65 ; i<91 ; i++) {
     int a[65-i] = char(i);
     cout<<a<<endl;

return 0;
}