473,386 Members | 1,846 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Need to loop through the letters of the alphabet.

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
2 12863
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: skinnybloke | last post by:
Hi I am trying to do something with PHP and having problems - a bit of a a PHP novice I'm afraid. What I am trying to achieve is to display the alphabet and numbers 0-9 on a web page. I want...
2
by: spidrw | last post by:
I have a system that lets users input service events (much like appointments at a Dr.'s office). Oftentimes a particular person will have 30 or 31 appointments in a month, as all events are...
5
by: Stefan Krah | last post by:
Hello, I am currently writing code where it is convenient to convert char to int . The conversion function relies on a character set with contiguous alphabets. int set_mesg(Key *key, char...
8
by: Jack Addington | last post by:
I want to scroll through the alphabet in order to scroll some data to the closest name that starts with a letter. If the user hits the H button then it should scroll to the letter closest to H. ...
31
by: Joe Smith | last post by:
"ABCDEFGHIJKLMNOPQRSTUVWXYZ" "abcdefghijklmnopqrstuvwxyz" "0123456789" " " "!#%^&*()-_" "+=~\|;:\'" "\"{},.<>/\?" "\a\b\f\n\r\t\v\\" Do the above string literals comprise an alphabet for C?...
3
by: JDubP | last post by:
I know a bit about VBA in Excel, but not about looping records in Access 2003, so any help will be appreciated. I think if someone can send me some sample code, I can muddle my way through. (or if...
3
by: sivadhanekula | last post by:
Hi all I am working on excel, Macros with vb6....When I was writing the for loop I got stuck with getting the next alphabet...Like I need to extract the data from a database and I need to split...
1
by: carter09 | last post by:
I need help writting a program. 1) Random string generation 2) no repeating letters Can anyone help me,please? I am so confused. The only problem is I have to use the code that is written...
4
by: abueno | last post by:
//It should count how many characters are letters in the English alphabet, and is displaying the correct letters, but is not counting good. void FunctionCountLetters(char s) { int len; int i;...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.