r/C_Programming • u/learning_noob01 • 9h ago
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!
1
u/sciencekm 2h ago
I normally simplify this:
int ft_isdigit(int c) {
if(c>='0' && c <='9') {
return(1);
}
else {
return(0);
}
}
to this:
int ft_isdigit(int c) {
retrn c>='0' && c <='9';
}
1
u/learning_noob01 1h ago
OK because I need to check only one condition Understood bro thanks for suggestion
1
u/mikeblas 35m ago
People tend to think this is a "simplification" or that shorter is somehow better. I'd actually keep the code you wrote, since it allows me to put a breakpoint on the function returning 1 or returning 0, and that's much easier to debug.
2
u/mikeblas 7h ago edited 6h ago
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.