r/C_Programming • u/learning_noob01 • Aug 10 '26
Review Beginner learning C via 42-style exercises — would appreciate a review of my ft_isalpha and ft_isdigit
Hey all — I've got a solid C++ background but I'm new to C specifically, working through it 42-school-style (strict norm: tabs not spaces, no for loops, variables declared at top of block, -Wall -Wextra -Werror, no libc shortcuts like the real isalpha/isdigit).
Just finished reimplementing isalpha and isdigit from scratch. Both compile clean and pass my own test cases (including boundary chars like '0' and '9'), but I'd genuinely appreciate a second pair of eyes — especially on anything that "works but isn't how a C dev would actually write it."
#include <stdio.h>
int ft_isalpha(int c);
void ft_putchar(char c);
int main(void)
{
int c1[5] = {'a','b','g','5','A'};
int count;
int c;
count =0;
while(count<5){
c = c1[count];
count++;
if(ft_isalpha(c) == 0){
ft_putchar('0');
}
else{
ft_putchar('1');
}
ft_putchar('\n');
}
return (0);
}
int ft_isalpha(int c){
if((c >= 'a' && c <='z') || (c >='A' && c <='Z')){
return (1);
}
else{
return (0);
}
}
void ft_putchar(char c){
putchar(c);
}
#########################################################################################
#include <stdio.h>
int ft_isdigit(int c);
int main(void)
{
int x;
int y[7] = {'a','1','2','b','c','0','9'};
int count;
count =0;
while (count < 7)
{
x = y[count];
if(f_isdigit(x) != 0){
putchar('1');
}
else
{
putchar('0');
}
putchar('\n');
count ++;
}
return (0);
}
int ft_isdigit(int c)
{
if(c>='0' && c <='9')
{
return(1);
}
else{
return(0);
}
}Want to make sure that reasoning is actually correct and not something I've half-convinced myself of.
Questions I have:
- Is there a cleaner/more idiomatic way to write either range check?
- Any norm/style conventions I'm likely missing that wouldn't show up until a real evaluation?
Not looking for someone to rewrite it for me — just want honest feedback on whether this is solid or if I'm building bad habits early. Thanks!
6
u/SmokeMuch7356 Aug 10 '26 edited Aug 11 '26
Oh God this bunch.
I have real issues with the way they teach C. They are stunting your education with these restrictions.
forloops are useful.do...whileloops are useful.switchstatements are useful. The standard library is useful. You can learn the basics without having to do the programming equivalent of starting a fire by rubbing two sticks together (that's what assembly language is for, and even assembly language gives you a match). It's just a stupid way to teach programming in general and the C language in particular.You haven't needed to declare everything at the head of a block since C99, and minimizing a variable's scope is a good thing.
Tabs pollute diffs, because even though this bunch mandates 4-space tabs, once you start doing this outside of class you're going to run into people who use 2- or 4- or 5- or 8-space tabs, so everybody uses different numbers of tab stops and you wind up with a tangled mess in the repo.
Okay, rant over. Topic:
ft_isdigitis okay; digit characters are consecutive in both ASCII and EBCDIC. Personally I'd write it asbut either way works.
ft_isalphais a bit incomplete. It works fine for base ASCII characters. It works for base EBCDIC, although be aware that alpha characters in EBCDIC are broken up into groups with some gaps between them -'a'(129) to'i'(137),'j'(145) to'r'(153), and's'(162) to'z'(169) (same groupings for uppercase). But it won't work for extended characters like'ñ'or'è'in either character set. The realisalphafunction has to take both character set and locale into account, so you'll want to read up on extended ASCII and code pages.This is why we don't write our own
isalphaandisdigitfunctions.EDIT
A table-driven approach works better. We create some bitmasks for each character class (space, punctuation, digit, etc.), then bitwise-OR them together for each character in our character set. Quick-n-dirty example:
And the output:
Would this be a pain in the ass to set up for every character in every code page we'd want to support? Yes. But, the payoff is a more flexible framework (you don't have to hack your
is...functions every time you add a new code page) and the code is faster (array lookup plus bitwise operation vs. a bunch of if statements).2
u/mikeblas Aug 10 '26 edited Aug 10 '26
You've double (triple?) Pasted your code. Might want to clean thst up. (OH, I see. Different tests. Seems an odd way to factor your code. Why not multiple test suites from one
main()calling function?)Thing is, these functions exist in the runtime. Nobody would rewrite them unless they couldn't use the runtime, for some reason. If you're writing tests, why not test every possible character [0..255] against the runtime implementation?
Your implementations are strictly ASCII, and won't work on Unicode characters. Maybe that's out of scope for your assignment. But then, why accept
intcharacters when you don't support them?Would these functions be materially different if you were implementing them in C++? I don't think so, which makes me curious about your context.
2
u/sciencekm Aug 10 '26
I normally simplify this:
to this:
1
u/rubidus-api Aug 12 '26 edited Aug 12 '26
One thing nobody's mentioned: your second file calls
f_isdigit(x), notft_isdigit(x). With-Wall -Wextra -Werrorthat doesn't build — implicit declaration — so what you pasted isn't what you compiled. Worth checking that the code you're asking people to review is the code you actually ran.On the
return (1);discussion above: inside 42 you don't have a choice. The Norm says "Return of a function has to be between parentheses", and norminette will flag you for dropping them. Outside 42 you'll almost never see those parens. Both pieces of advice are right, just for different rooms.+1 to what SmokeMuch7356 said about EBCDIC, and it cuts the other way for digits: the standard actually guarantees
0–9are contiguous (C17 §5.2.1) and guarantees nothing of the sort for letters. Soft_isdigitis portable andft_isalphaisn't, and that asymmetry is the interesting part.And since a few people have brought up Unicode: the reason nobody writes their own
isalphafor real text isn't only the locale tables. Once you leave ASCII, "is this a letter" drags in normalization — the same visible letter can be one code point, or a letter plus a combining accent, so NFC and NFD compare unequal byte for byte — and overlong UTF-8 encodings, the classic way a decoder gets talked into letting a/slip past a filter that already checked the string. Then there are homoglyphs — two identifiers that are pixel-for-pixel identical on screen and different in bytes — and bidirectional control characters, which reorder how a line is displayed without touching what the compiler reads, so a comment can appear to swallow the code after it. Source that says one thing to a human and another to the compiler.None of that is your problem today. But asking about the range check in the first place is the right instinct, and I'd take it as a good sign: that check is exactly where the question "what counts as a character here?" starts. The people who get bitten by the rest of it later are the ones who never thought to ask.
Taking the character as an
intis good instinct too. As non-existing-person said, the return ofgetchar()and friends has to go into anintrather than achar, or you can't tell EOF from a real character — and in C, unlike C++, a character constant like'a'is anintto begin with. That said, nothing here is reading fromgetchar(), so some people would feel achararray was the honest choice for this particular test harness.Good luck with the rest of the course!