The program is written to bubble sort the dates inputted by the user in dd/mm/yyyy format where the month can be a string or an integer. No error is shown after compilation but segmentation fault error shows up after inputting the dates during execution. I'm unable to figure out the mistake. Also, I'm relatively new to programming. Please help.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void readElements(char **date, int n) {
printf("enter the dates in dd/mm/yyyy format where month can be a string or "
"an integer\n");
for (int i = 0; i < n; i++) {
date[i] = (char *)malloc(50);
scanf("%s", date[i]);
}
}
char strcompare(char *a, char *b, int n, int length1, int length2) {
char *sub1, *sub2, *sub3, *sub4;
int c = 0, d = 0, e = 0, f = 0;
// comparing year
while (c < 4) {
sub1[c] = a[length1 - 4 + c];
c++;
}
while (d < 4) {
sub2[d] = b[length2 - 4 + c];
d++;
}
if (strcmp(sub1, sub2) > 0) {
return 'o';
} else if (strcmp(sub1, sub2) < 0) {
return 'z';
} else
// comparing month
{
for (int i = length1 - 6; i > 2; i--) {
sub3[e] = a[i];
e++;
}
for (int i = length2 - 6; i > 2; i--) {
sub4[f] = b[i];
f++;
}
if (strcmp(sub3, sub4) == 0) {
int g = 0, h = 0;
// comparing day
char *sub5, *sub6;
while (g < 2) {
sub5[g] = a[g];
g++;
}
while (h < 2) {
sub6[h] = b[h];
h++;
}
if (strcmp(sub5, sub6) > 0) {
return 'o';
} else {
return 'z';
}
} else {
if (sub3 == "january") {
strcpy(sub3, "01");
}
if (sub4 == "january") {
strcpy(sub4, "01");
}
if (sub3 == "february") {
strcpy(sub3, "02");
}
if (sub4 == "february") {
strcpy(sub4, "02");
}
if (sub3 == "march") {
strcpy(sub3, "03");
}
if (sub4 == "march") {
strcpy(sub4, "03");
}
if (sub3 == "april") {
strcpy(sub3, "04");
}
if (sub4 == "april") {
strcpy(sub4, "04");
}
if (sub3 == "may") {
strcpy(sub3, "05");
}
if (sub4 == "may") {
strcpy(sub4, "05");
}
if (sub3 == "june") {
strcpy(sub3, "06");
}
if (sub4 == "june") {
strcpy(sub4, "06");
}
if (sub3 == "july") {
strcpy(sub3, "07");
}
if (sub4 == "july") {
strcpy(sub4, "07");
}
if (sub3 == "august") {
strcpy(sub3, "08");
}
if (sub4 == "august") {
strcpy(sub4, "08");
}
if (sub3 == "september") {
strcpy(sub3, "09");
}
if (sub4 == "september") {
strcpy(sub4, "09");
}
if (sub3 == "october") {
strcpy(sub3, "10");
}
if (sub4 == "october") {
strcpy(sub4, "10");
}
if (sub3 == "november") {
strcpy(sub3, "11");
}
if (sub4 == "november") {
strcpy(sub4, "11");
}
if (sub3 == "december") {
strcpy(sub3, "12");
}
if (sub4 == "december") {
strcpy(sub4, "12");
}
if (strcmp(sub3, sub4) > 0) {
return 'o';
} else {
return 'z';
}
}
}
}
void Bubblesort(char **date, int n) {
int i, j;
char *t;
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - i - 1; j++) {
int length1 = strlen(date[j]);
int length2 = strlen(date[j + 1]);
if (strcompare(date[j], date[j + 1], n, length1, length2) == 'o') {
t = date[j];
date[j] = date[j + 1];
date[j + 1] = t;
}
}
}
}
void printarray(char **date, int n) {
printf("Sorted array:\n");
for (int i = 0; i < n; i++) {
printf("%s\n", date[i]);
}
}
void main() {
int n;
char *date[50];
printf("enter the number of elements that are to be sorted\n");
scanf("%d", &n);
readElements(date, n);
Bubblesort(date, n);
printarray(date, n);
}