How to resolve the algorithm Palindrome dates step by step in the C programming language
How to resolve the algorithm Palindrome dates step by step in the C programming language
Table of Contents
Problem Statement
Today (2020-02-02, at the time of this writing) happens to be a palindrome, without the hyphens, not only for those countries which express their dates in the yyyy-mm-dd format but, unusually, also for countries which use the dd-mm-yyyy format.
Write a program which calculates and shows the next 15 palindromic dates for those countries which express their dates in the yyyy-mm-dd format.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Palindrome dates step by step in the C programming language
This C code finds and prints the next 15 palindrome dates, which are dates that read the same forwards and backwards. Here's a detailed explanation:
-
Header Inclusions:
<stdbool.h>
: Includes thebool
data type.<stdio.h>
: Provides input/output functions.<string.h>
: Contains string manipulation functions.<time.h>
: For date and time functions.
-
is_palindrome
Function:- This function checks if a given string
str
is a palindrome (reads the same forwards and backwards). - It uses a loop to compare characters at corresponding positions from the beginning and end of the string.
- If any pair of characters doesn't match, it returns
false
. - Otherwise, it returns
true
.
- This function checks if a given string
-
main
Function:- Variables:
time_t timestamp
: Stores the current timestamp.const int seconds_per_day
: Represents the number of seconds in a day.int count
: Keeps track of the number of palindrome dates to find (initially set to 15).char str[32]
: A buffer to store formatted dates.
- Output:
- The program prints the heading "Next 15 palindrome dates:".
- Loop:
- It enters a loop that continues as long as
count
is greater than 0. - Inside the loop, the timestamp is incremented by
seconds_per_day
to move to the next day. - The
gmtime
function is used to convert the timestamp to astruct tm
representing the date and time. - The
strftime
function formats the date into thestr
buffer using the format "%Y%m%d", which represents the date in YYYYMMDD format. - The
is_palindrome
function is called to check if the formatted date is a palindrome. - If it's a palindrome, the date is formatted using the "%F" format (YYYY-MM-DD) and printed.
- The
count
is decremented.
- It enters a loop that continues as long as
- Variables:
-
Return:
- The
main
function returns 0, indicating successful program execution.
- The
This program iterates through the next 15 days starting from the current time, checking if each day's date is a palindrome, and prints the palindrome dates it finds.
Source code in the c programming language
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
bool is_palindrome(const char* str) {
size_t n = strlen(str);
for (size_t i = 0; i + 1 < n; ++i, --n) {
if (str[i] != str[n - 1])
return false;
}
return true;
}
int main() {
time_t timestamp = time(0);
const int seconds_per_day = 24*60*60;
int count = 15;
char str[32];
printf("Next %d palindrome dates:\n", count);
for (; count > 0; timestamp += seconds_per_day) {
struct tm* ptr = gmtime(×tamp);
strftime(str, sizeof(str), "%Y%m%d", ptr);
if (is_palindrome(str)) {
strftime(str, sizeof(str), "%F", ptr);
printf("%s\n", str);
--count;
}
}
return 0;
}
You may also check:How to resolve the algorithm Short-circuit evaluation step by step in the Prolog programming language
You may also check:How to resolve the algorithm Simple turtle graphics step by step in the Perl programming language
You may also check:How to resolve the algorithm Find common directory path step by step in the J programming language
You may also check:How to resolve the algorithm Rot-13 step by step in the X86 Assembly programming language
You may also check:How to resolve the algorithm Draw a clock step by step in the AWK programming language