See the following code.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int isPalindromePossible(int);
unsigned concatenate(unsigned, unsigned);
int jour, mois, annee, jm, length, leng, begin, end;
char str[12], str2[12], revstr[40];
int main() {
printf("Entrez le jour : ");
scanf("%d", &jour);
printf("Entrez le mois : ");
scanf("%d", &mois);
printf("Entrez l'annee : ");
scanf("%d", &annee);
int test = reverse(annee);
isPalindromePossible(annee);
return 0;
}
int isPalindromePossible(int year) {
int firstHalf;
int secondHalf;
while (year > 0) {
int digit = year % 10;
printf("YEAR = %d\n", year);
if (year <= 99) {
concatenate(secondHalf, digit);
} else {
concatenate(firstHalf, digit);
}
year = year / 10;
}
printf("FH = %d, SH = %d", firstHalf, secondHalf);
return 0;
}
unsigned concatenate(unsigned x, unsigned y) {
unsigned pow = 10;
while(y >= pow)
pow *= 10;
return x * pow + y;
}
The code was ran, and output this.
See how the second half never gets filled, even tho the if statement is working. I'm scratching my head and can't figure out why.
If you see the problem I'd appreciate.
Many thanks.
Both of firstHalf
and secondHalf
are used without being initialized. You invoked undefined behavior by using values of uninitialized non-static local variables, which are indeterminate.
You have to
concatenate
.int isPalindromePossible(int year) {
int firstHalf = 0;
int secondHalf = 0;
while (year > 0) {
int digit = year % 10;
printf("YEAR = %d\n", year);
if (year <= 99) {
secondHalf = concatenate(secondHalf, digit);
} else {
firstHalf = concatenate(firstHalf, digit);
}
year = year / 10;
}
printf("FH = %d, SH = %d", firstHalf, secondHalf);
return 0;
}