473,781 Members | 2,413 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.WriteLi ne(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*******@newsg roup.nospam
Feb 10 '08 #1
2 12943
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.WriteLi ne(prfx + Convert.ToChar( i));
i += 1;
if (loop == 26)
{
loop = 0;
prfx = Convert.ToChar( iPrfx).ToString ();
iPrfx += 1;
i = 97;
}
}

"moondaddy" <mo*******@news group.nospamwro te in message
news:uG******** ******@TK2MSFTN GP06.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.WriteLi ne(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*******@newsg roup.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
1704
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 the letters/numbers to be clickable to specific web pages if there are companies on my database that start with that letter. If they are not on the database I just wantthe letters displayed but not clickable.
2
1590
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 consecutive. All that is logged is days of service, not duration or anything. For our contractual reporting purposes, I used to be able to simply input 30 (or 31) days of service if someone visited every day for a month, but now I must create...
5
6658
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 *s) { char *x;
8
3262
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. If no one exists with H, then go to I, etc. If its near the end, say 'V', and the last person is a 'T' then it should work its way back up the alphabet. I was trying to loop as if the Char's were ints but I am having problems I have buttons...
31
2680
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? joe
3
2462
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 you can send me alink to read how to do it that is fine also) I presently run a macro that: 1) runs a query where User has to input a value for field and 2) prints/saves a report in snapshot view (i have to manually type in file name) I...
3
3083
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 that data in to equal parts and each part should go to diff column in the excel sheet like A, B, C, D.....Z if you look at the code in the code if you look at the Bold letters...and when the loop is running I need the Next alphabet in the place...
1
1493
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 there but add on to it. import random alphabet = "abcdefghijklmnopqrstuvwxyz" myNewString = ""
4
2690
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; len=strlen(s); cout<<"\n The lenght of the string is= "<<len<<endl;
0
9639
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9474
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10308
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10143
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9939
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5507
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4040
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3633
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2870
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.