90 Snippets: Clang ๐จโ๐ป
90 Projects towards mastery - Day 10 ๐ง๐ปโโ๏ธ
Hello there, fellow coding enthusiasts! It's Astro, and today, I'm excited to share my latest coding adventure with you. As I continue my journey through the world of programming, I've set my sights on the fascinating realm of C. This time, I've taken on the challenge of creating a program that can detect palindromes โ those intriguing strings that read the same forwards and backward. So, join me as we dive into this C odyssey and explore the magic of palindromes!
The Goal: Decoding Palindromes
At the heart of this endeavor lies a simple yet intriguing goal: to determine whether a given string is a palindrome. Palindromes, like "racecar" and "madam," have a symmetrical charm that captures our curiosity. The challenge here is to create a program that can recognize these linguistic wonders.
The Approach: String Reversal and Comparison
To achieve this, I've adopted a straightforward approach. I'm employing two key functions that make this journey possible: dup_rev
and is_palindrome
.
The dup_rev
Function
char *dup_rev(char *s)
{
int i;
int j;
char *rev;
if (!s)
return (NULL);
rev = calloc(strlen(s) + 1, sizeof(char));
if (!rev)
return (NULL);
i = -1;
j = strlen(s) - 1;
while (s[++i])
rev[i] = s[j--];
return (rev);
}
This function performs the art of string reversal. Given an input string, it creates a reversed copy of it. This is achieved by carefully allocating memory, looping through the characters, and manipulating strings. Think of it as creating a mirror image of the input string.
The is_palindrome
Function
bool is_plaindrome(char *s)
{
char *rev;
bool res;
if (!s)
return (false);
rev = dup_rev(s);
res = !strncmp(s, rev, strlen(s));
free(rev);
return (res);
}
Building upon the foundation laid by dup_rev
, the is_palindrome
function takes the original input and its reversed counterpart. It then compares the two to determine if the string is indeed a palindrome. If the two versions match, we've discovered a palindrome!
The Code: Unveiling the Logic
int main(int ac, char **av)
{
if (ac != 2)
return (1);
if (is_plaindrome(av[1]))
printf("\"%s\" is a plaindrome\n", av[1]);
else
printf("\"%s\" is not a plaindrome\n", av[1]);
return (0);
}
Now, let's peek at the code without overwhelming you. The dup_rev
function works its magic by creating a reversed version of the input string. The is_palindrome
function takes the original and reversed strings to decide if it's a palindrome. With these tools in hand, we're ready to explore the world of palindromes.
The Makefile: Navigating Compilation
SRC = plaindrome.c
NAME= is_plaindrome
CC = clang
FLAGS = -Wall -Wextra -Werror
all: $(NAME)
$(NAME): $(SRC)
@$(CC) $(FLAGS) $(SRC) -o $(NAME)
@echo "Compiled is_plaindrome"
clean:
@rm -rf $(NAME)
@echo "Removed is_plaindrome"
fclean: clean
A Makefile accompanies our journey, ensuring smooth compilation. It takes the source code, compiles it using the Clang compiler, and generates the program. This behind-the-scenes helper ensures that our code is ready to run without a hitch.
Testing: Trying It Out
INPUT: lol
OUTPUT: "lol" is a plaindrome
INPUT: lo
OUTPUT: "lo" is not a plaindrome
INPUT: lol
OUTPUT: "racecar" is a plaindrome
INPUT: hashnode
OUTPUT: "hashnode" is not a plaindrome
Conclusion: Unleashing the C Magic
And that wraps up our exploration into palindromes with C. This coding quest has given me a deeper appreciation for the intricacies of strings, memory management, and logical thinking. Every step has been a stride forward in my coding journey, paving the way for even more exciting challenges.
As I venture further into the world of programming, I'm eagerly anticipating the lessons, the revelations, and the countless lines of code waiting to be written. Remember, fellow coders, each challenge is a chance to grow, learn, and expand our coding horizons. Until next time, keep coding and keep exploring!