My solution for this month’s Exercise didn’t require much work. What I did was to “stack overflow” the problem by pulling in functions from earlier Lessons. The only new portion of code deals with processing the input before sending the string off to the b36_decimal() function I’ve already written.
Oh, and the b36_decimal() function also needs the base36_powers() function, if you opt to code the solution as I did:
2025_08-Exercise.c
#include <stdio.h> #include <ctype.h> #define BASE36_MAX 36 #define SIZE 10 /* build the base 36 powers table */ long *base36_powers(void) { static long b36_powers[SIZE]; int x; for( x=0; x<SIZE; x++ ) b36_powers[x] = x ? b36_powers[x-1]*BASE36_MAX : 1; return(b36_powers); } /* return decimal value for base 36 string v */ long b36_decimal(char *v) { long *powers,result; int x,y,digit; /* obtain the powers table */ powers = base36_powers(); /* must process the string backwards */ x = 0; while( *(v+x) != '\0' ) /* locate the string's end */ x++; x--; /* backup to 1's position */ /* process the digits (x) and powers (y) */ result = 0; y = 0; while(x>=0) { /* translate digit */ if( *(v+x)>='0' && *(v+x)<='9' ) digit = *(v+x) - '0'; else digit = *(v+x) - 'A' + 10; /* fetch power value */ result += digit * *(powers+y); x--; y++; } return(result); } int main() { char name[32]; long r; int x; /* prompt for input */ printf("Enter your name: "); fgets(name,32,stdin); /* remove newline and set to uppercase */ x = 0; while(x<32) { if( name[x]=='\n' ) { name[x] = '\0'; break; } name[x] = toupper(name[x]); x++; } /* translate the b36 name into decimal */ r = b36_decimal(name); printf("Base 36 %s in decimal is %ld\n",name,r); /* clean-up */ return 0; }
The user is prompted for a name in the main() function. I use the fgets() function for input, which is safe. The while loop pulls the newline from the end of input as well as converts letters to uppercase. I don’t perform any further checks, so the user could input anything. Nothing serious happens when invalid input flies in.
Here’s a sample run:
Enter your name: danny
Base 36 DANNY in decimal is 22332238
Yes, this Exercise isn’t much of a challenge, because it’s merely cobbling together existing code. Still, that’s what a lot of coders do and I think doing so is okay — if it’s your own code! For heaven’s sake, don’t let AI do the job if you ever expect to learn anything or to become a valuable resource as a programmer.