Check If A String Is A Palindrome Using Recursion | C Programming Example

preview_player
Показать описание
Рекомендации по теме
Комментарии
Автор

Dr. Browne,

I have followed many tutorials on YouTube for learning programming languages and nobody comes close to your "C Programming Tutorials" series! I already thought you were the best programming instructor on YouTube, and when I came across your "C Programming Examples" series, I was stunned to find more, top-notch resources provided by you. When I finally land a job as an embedded software engineer, sending a token of appreciation your way will be a top priority of mine!

Thank you for all you do, Dr. Browne! You are simply unrivaled when it comes to teaching programming--I hope to one day learn C++ and Python from your other series!

MW-vkes
Автор

What better way to end a Friday than to see that the da goat of C posted a video 😁

This is a great example to begin wrapping your head around recursion.

Thank you!

ivanpalacio
Автор

I love your video lecturers, very clean and intuitive. Thanks

vukimduykhcm
Автор

Bro you've just saved my life right now

lonewolf.
Автор

Nice! I always struggle what to use in this cases, recursion of loops/iteration. I prefer the latter. What is your point of view?
Here my pointer version:

#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#define BUFFSIZE 255
bool is_palindromic(char *str);

int main(int argc, char *argv[])
{
if (argc < 2)
{
printf("Enter a string (max. 254 characters) on the command line\n");
return 1;
}
char str[BUFFSIZE];
strncpy(str, argv[1], BUFFSIZE);
printf((is_palindromic(str) ? "%s is palindromic\n" : "%s is not palindromic\n"), str);
return 0;
}

bool is_palindromic(char *str)
{
char *ptr_left, *ptr_right;

// Find last char of string.
ptr_right = str;
while (*ptr_right != '\0')
++ptr_right;
--ptr_right;

// Test if left and right chars are equal.
ptr_left = str;
while (ptr_left < ptr_right)
{
printf("%c - %c\n", *ptr_left, *ptr_right);
if (*ptr_left++ != *ptr_right--)
return false; // Not palindromic
}
return true; // Palindromic
}

marccalis