Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I write a single for loop running from a to z and A to Z in C?

Tags:

c

I want to combine both the for loops into single for loop. How can i do that?

I want to loop through a to z, and A to Z, like so:

char ch;
for (ch = 'A' ; ch <= 'Z' ; ch++ )
{ 
}
for (ch = 'a' ; ch <= 'z' ; ch++ )
{
}

but using a single loop.

like image 692
kobe Avatar asked Jun 15 '11 04:06

kobe


People also ask

How do you loop A to Z in Python?

Using String module ascii_lowercase method returns all lowercase alphabets as a single string abcdefghijklmnopqrstuvwxyz so the program is simply running a for loop over the string characters and printing them. Similarly for uppercase A to Z letters.


1 Answers

for (char ch = 'A' ; ch <= 'z' ; ch == 'Z' ? ch = 'a' : ++ch )
{
}

Should work -- though please, please, don't inflict this on your fellow developers.

like image 100
Billy ONeal Avatar answered Jan 01 '23 19:01

Billy ONeal