I don’t believe one definitive solution exists for this month’s Exercise: Output a string of upper- and lowercase letters. No, the challenge is more to discover different insights a coder has to a specific problem.
Here is the desired output string:
AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz
My first approach is one I consider the most obvious:
2022_08-Exercise-a.c
#include <stdio.h> int main() { int x; for( x='A'; x<='Z'; x++ ) printf("%c%c",x,x+32); putchar('\n'); return(0); }
A for loop plows through characters 'A'
through 'Z'
at Line 7. The character is output at Line 8, along with its companion lowercase letter, which is x+32
(thank you ASCII table). This solution was my first stab, though it could also be done by using two variables instead:
2022_08-Exercise-b.c
#include <stdio.h> int main() { int x,y; for( x='A',y='a'; x<='Z'; x++,y++ ) printf("%c%c",x,y); putchar('\n'); return(0); }
Here both x
and y
are initialized in the for loop. Only one terminating condition needs to be specified, x<='Z'
. Then both x
and y
are incremented each turn. This solution avoids math in the printf() statement, but it requires a knowledge that you can perform multiple actions within a single for loop statement.
This next solution uses a while loop and a single char variable alphabet
:
2022_08-Exercise-c.c
#include <stdio.h> int main() { char alphabet = 'A'; while( alphabet!='Z'+1) { putchar(alphabet); putchar(alphabet+32); alphabet++; } putchar('\n'); return(0); }
The while loop spins as long as the value of alphabet
doesn't equal 'Z'+1
. This expression allows letters Z and z to be output, because the loop stops after Z is output. The putchar() at Line 10 adds 32 to the value of alphabet
, generating lowercase letter output. Most importantly, alphabet
is incremented at Line 11, which keeps the loop moving.
Now I get bizarre:
2022_08-Exercise-d.c
#include <stdio.h> int main() { /* size the array for two alphabets (26) and the null character */ char a[26*2+1]; char alpha; int x; /* do uppercase */ alpha = 'A'; for( x=0; x<26*2; x+=2 ) { a[x] = alpha; alpha++; } /* do lowercase */ alpha = 'a'; for( x=0; x<26*2; x+=2 ) { a[x+1] = alpha; alpha++; } /* append the null char */ a[26+26] = '\0'; /* output result */ puts(a); return(0); }
Two for loops fill array a[]
. The first sets uppercase letters to every other position in the array. The second for loop works similarly, but for lowercase and using a[x+1]
as its index value. The +1
shifts the lowercase letters over, squeezing them between uppercase letters already set in the array. It's kinda crazy, but it doesn't attain the level of insanity as this version:
2022_08-Exercise-e.c
#include <stdio.h> #include <stdlib.h> int main() { const int size = 26*2+1; char *a; int x,y; /* allocate storage for two alphabets plus a null character */ a = malloc( sizeof(char) * size ); if( a==NULL ) { fprintf(stderr,"Unable to allocate memory\n"); exit(1); } /* initialize the buffer to all As */ for( x=0; x<size; x++ ) *(a+x) = 'A'; /* manipulate contents */ for( x=0,y=0; x<26*2; x+=2,y++ ) { *(a+x) = *(a+x) + y; *(a+x+1) = *(a+x+1) + y + 32; } /* output the results */ *(a+size-1) = '\0'; puts(a); return(0); }
My nuttiest solution uses a pointer, allocates storage, then fills the buffer with uppercase letter 'A'
. The action takes place at Line 25, where a for loop uses two variables to process the buffer. The first looping variable x
is increased by two each turn; variable y
incremented by one.
The statements at Lines 27 and 28 adjust the value of character 'A'
(already in the buffer) to create the mixed upper- lowercase string. It took a few tries for me to code something so convoluted, but it works.
I hope you came up with a working solution, easy or hard. If you're an experienced coder, I hope you wove a tapestry of statements that inspires and delights. That's the point of the exercise.