By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,825 Members | 1,281 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,825 IT Pros & Developers. It's quick & easy.

Need to loop through the letters of the alphabet.

P: n/a
Using c# 3.0:
I need to do a loop and for each iteration I need to call the next letter of
the alphabet. Sometimes this code may only loop 10 times, and other cases
it may loop 100 times.

I need to do something line this:

for (int i = 1; i <= X; i++)

{

Console.WriteLine(nextLetter);

}

And the result would be like this:

a

b

c

d

e

etc.

After the 26th loop the results would be like this:

aa

ab

ac

ad

ae

etc.

any ideas how I might achieve this?

Thanks.
--
mo*******@newsgroup.nospam
Feb 10 '08 #1
Share this Question
Share on Google+
2 Replies


P: n/a
I figured it out:

If anyone has a better way, please let me know. Thanks.

int i = 97; // initialize i to the char value of 'a'
string prfx = "";
int loop = 0;
int iPrfx = 97;
for (int iCnt = 0; iCnt <= 100; iCnt++)
{
loop += 1;
Console.WriteLine(prfx + Convert.ToChar(i));
i += 1;
if (loop == 26)
{
loop = 0;
prfx = Convert.ToChar(iPrfx).ToString();
iPrfx += 1;
i = 97;
}
}

"moondaddy" <mo*******@newsgroup.nospamwrote in message
news:uG**************@TK2MSFTNGP06.phx.gbl...
Using c# 3.0:
I need to do a loop and for each iteration I need to call the next letter
of the alphabet. Sometimes this code may only loop 10 times, and other
cases it may loop 100 times.

I need to do something line this:

for (int i = 1; i <= X; i++)

{

Console.WriteLine(nextLetter);

}

And the result would be like this:

a

b

c

d

e

etc.

After the 26th loop the results would be like this:

aa

ab

ac

ad

ae

etc.

any ideas how I might achieve this?

Thanks.
--
mo*******@newsgroup.nospam

Feb 10 '08 #2

P: n/a
Moondaddy,

I find this nicer

//the vars just for fun because you said C# 3.0
var x = new string[26];
for (var y = 0; y < 2; y++)
{
for (var i = 0; i < 26; i++)
{
x[i] += Convert.ToChar(i + 97);

}
}

For others be aware this goes only for West European languages where the
alphabeth has 26 characters.

Cor

Feb 10 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.