+ 1

How to do convert US date to EU date in C

I have tried it 3-4 times and unable extract anything out of my mind to give it a structure hope so anyone whose expertise is in C can tackle this one

10th May 2021, 2:21 PM
Vishal
Vishal - avatar
6 odpowiedzi
+ 2
The case that is most difficult is with month name. Were you able to handle the cases containing "/" for inputs? The least difficult part is tokenizing the string. You can use string.h's strtok to get the pieces. The hardest part is associating all the month names to month numbers in c. I think the shortest approach with that involves an array of month names and looping through them to loop up index by month name. Even if this doesn't seem complex to you, it adds the most code. Below, I show the array I used. I just specified enough of the month names to uniquely identify the month. I didn't compare the full month name. The problem doesn't require this but I also ignored case of the input by converting all characters of input to lower case. My approach ended up being this: #include <stdio.h> #include <ctype.h> #include <string.h> #define NUM_MONTHS 12 const char * months[NUM_MONTHS] = { "jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sept", "oct", "nov", "dec" }; int main() { char line[256]; fgets(line, 255, stdin); printf("%d/%d/%d", getDay(line), getMonthNumber(line), getYear(line)); return 0; } I left out definitions of getDay, getMonthNumber, and getYear to not spoil it for you. I implemented those functions to handle the 2 possible input formats. I found this to be a good way to break the problem down into isolated pieces that were easy enough to thoroughly test separately.
12th May 2021, 6:33 PM
Josh Greig
Josh Greig - avatar
+ 1
Vishal, for your drivers license test case that failed, try this: Bob 1 Bae Bella By Tom Your output: 20 Expected output: 60 (based on my solution that passes all test cases) I think your comparison for names before only looks at the first letter. The above test case has a couple names before Bob with the same letter which is why your code fails.
14th May 2021, 11:25 AM
Josh Greig
Josh Greig - avatar
0
Thank you Sir
13th May 2021, 3:19 PM
Vishal
Vishal - avatar
0
Josh Greig Sir, I am having problem in New drivers license (code coach) also that I wrote in C below. My all test cases got passed except for the 5th one. can you tell me why it's not getting passed. #include <stdio.h> #include <string.h> int main() { char arr[20]; char arr2[50]; int num; scanf("%s\n %d\n",arr,&num); fgets(arr2,sizeof(arr2), stdin); int s= strlen(arr2); int min = 20; int plus =0; int d; for(int i=0; i<s; i++){ if(arr2[i] >= 'A' && arr2[i] <= 'Z'){ if(arr[0] > arr2[i] && num == 1){ plus += 20; } } } d= min + (plus/num); printf ("%d", d); return 0; }
13th May 2021, 3:22 PM
Vishal
Vishal - avatar
0
Josh Greig By the way You have explained it very well As to to make a program more efficient. above test case of yours should also need to pass But I think I have solved it without solving your test case of 'Bob' When I changed it a little bit It passed Still it does not pass your case but how you explained it very well to me should be appreciated Thank you Sir for(int i=0; i<s; i++){ if(arr2[i] >= 'A' && arr2[i] <= 'Z'){ if(arr[0] > arr2[i] && num == 1){ plus += 20; }else if(arr[0] > arr2[i] && num == 3){ plus += 20; } } }
14th May 2021, 2:30 PM
Vishal
Vishal - avatar
0
Also, are you a software Engineer in any tech giant like Google Facebook or Microsoft. I am just curious!!!
14th May 2021, 2:32 PM
Vishal
Vishal - avatar