Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Quickest way to enumerate the alphabet

Tags:

c#

alphabet

I want to iterate over the alphabet like so:

foreach(char c in alphabet) {  //do something with letter } 

Is an array of chars the best way to do this? (feels hacky)

Edit: The metric is "least typing to implement whilst still being readable and robust"

like image 267
Ben Aston Avatar asked Feb 05 '10 16:02

Ben Aston


People also ask

What is the 27th letter in the alphabet?

Total number of letters in the alphabet Until 1835, the English Alphabet consisted of 27 letters: right after "Z" the 27th letter of the alphabet was ampersand (&). The English Alphabet (or Modern English Alphabet) today consists of 26 letters: 23 from Old English and 3 added later.

What are the 26 letters ofthe alphabet?

There are 26 letters in the English alphabet which range from 'a' to 'z' (with b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, and y in between).

How do I get a list of all letters in python?

The easiest way to load a list of all the letters of the alphabet is to use the string. ascii_letters , string. ascii_lowercase , and string. ascii_uppercase instances.

Why is the alphabet in that order?

While the numbers were lost over time, the letters and their order remain. Psychology Today posts that it had something to do with how easy it is to memorize with its bouncy cadence. Another theory is that the letters were put in order as part of a memory tool so people could memorize all 26 of them.


1 Answers

(Assumes ASCII, etc)

for (char c = 'A'; c <= 'Z'; c++) {     //do something with letter  }  

Alternatively, you could split it out to a provider and use an iterator (if you're planning on supporting internationalisation):

public class EnglishAlphabetProvider : IAlphabetProvider {     public IEnumerable<char> GetAlphabet()     {         for (char c = 'A'; c <= 'Z'; c++)         {             yield return c;         }      } }  IAlphabetProvider provider = new EnglishAlphabetProvider();  foreach (char c in provider.GetAlphabet()) {     //do something with letter  }  
like image 92
Richard Szalay Avatar answered Sep 19 '22 14:09

Richard Szalay