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

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

I want to scroll through a to z and from A to Z, for example:

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

but using one loop.

+8
c
source share
13 answers

I personally do not like this solution, but:

 char * letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; for (char * ptr = letters; *ptr != 0; ++ptr) { char ch = *ptr; ... } 
+21
source share
 for (char ch = 'A' ; ch <= 'z' ; ch == 'Z' ? ch = 'a' : ++ch ) { } 

Should work - although please, please do not force this on your fellow developers.

+22
source share

You can do this in a nested loop (two loops, but only one body):

 for (start = 'A'; start <= 'a'; start += 'a' - 'A') { end = start + 'Z' - 'A'; for (ch = start; ch <= end; ++ch) { /* body */ } } 
+3
source share

Well, the obvious question is why? ... and the second question: you don't like character sets other than ASCII (since your two loops don't work for EBCDIC), but a quick and dirty way to connect these two elements is

 for (ch = 'A'; ch <= 'z'; ch++) { if (ch > 'Z' && ch < 'a') ch = 'a'; : 
+3
source share
 const char diff = 'a' - 'A'; for (ch = 'A' ; ch <= 'Z' ; ch++ ) { char small_ch = ch + diff; //... } 
+2
source share

Try the following:

  for (int i = 0; i < 52; ++i) printf("%c\n", 'A' + i + ('a' - 'Z' - 1) * (i/26)); 
+1
source share
 for (int i=0;i<26;++i) { char ch = 'A' + i; // Logic for Uppercase letters. char ch1 = 'a' + i; // Logic for Lowercase letters. } 
0
source share
 #include<stdio.h> #include<conio.h> int main() { int i; char e; for(i=65;i<=122;i++) { if(i<91||i>96) { e=i; printf("%c\n",e); } } getch(); } 
0
source share

A simple solution

 int i; for(i = 0; i < 52; i++){ char ch = i + (i < 26? 'A' : 'a'); /* do something with ch */ } 

although I prefer, especially in sensible languages ​​that allow nested functions,

 for(ch = 'A'; ch <= 'Z'; ch++) dosomething(ch); for(ch = 'a'; ch <= 'z'; ch++) dosomething(ch); 

PS Kobe, I see in one of your comments that your reason for loops is to check if a character is a letter ... but a loop is a terrible way to do this. You can just do

 if(('A' <= c && c <= 'Z') || ('a' <= c && c <= 'z')){ /* c is a letter */ } 

or, which is much better

 #include ctype.h ... if(isalpha((unsigned char)c)){ /* c is a letter */ } 

(To understand why this is necessary, read the isalpha man page and the C language standard. This is one of several disgusting aspects of C.)

0
source share
 for (char ch = 'A'; ch <= 'z'; ch = ch == 'Z'?'a':ch+1) { //loop body } 

This approach is similar to Billy's, but with a slightly less unpleasant loop statement. I would not mind putting it on another developer, although I could write an increment statement as a function to clarify whether the increment operator was more complex:

 char nextChar(char c) { return c == 'Z' ? 'a' : c+1; } for (char ch = 'A'; ch <= 'z'; ch = nextChar(ch)) { //loop body } 
0
source share

This is pointless, just use one for the loop. Variables are mainly used as counters or bounds for a loop, so it knows when to stop working. Just do one loop. Two loops are completely unnecessary.

-one
source share
 void main() { char ch; for(ch= 65 ; ch <= 122 ; ch++) { if ( ch>=91 && ch<=96) continue; //hey check this out if this helps printf("%c \n",ch); } } 
-one
source share
 for (ch = 'A';ch <= 'z';ch++) { } 

It will also contain several characters, such as '[' and ']' ...

-2
source share

All Articles