Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define STRING_LEN 200

int main(){
    FILE * fp1 = fopen("file.csv", "r");
    char string[STRING_LEN], pinFind[STRING_LEN];
    char * pinFound = NULL;
    int day, mo, yr;

    printf("Enter the pin: ");
    scanf("%s", pinFind);

    while(fgets(string, STRING_LEN, fp1)){
      pinFound = strstr(string, pinFind);
      if(pinFound){
        char *p = strrchr(string, ','),tmpstr[STRING_LEN];
        if(sscanf(p + 1, "%u-%u-%4u", &day, &mo, &yr))
        printf("%s", p+1);
      }
    }
    return 0;
}


What I have tried:

In this code, I am trying to find the linear search to find the latest date like the given result is this.

18-10-2020
19-10-2020


but the latest one is the last one. So I just want to print that last one using linear search but don't know how to do it?
Posted
Updated 20-Oct-20 8:22am

1 solution

Keep a "maximum-so-far" date which you preset to minimum possible date before the loop.
Inside the loop, you check the current line's date against the maximum-so-far, and if it is greater, set the new maximum-so-far.
After the loop, print maximum-so-far.

For positive integers in an array:
C++
int max = -1;
for (int i = 0; i < noOfElements; i++)
   {
   if (arr[i] > max)
      {
      max = arr[i];
      }
   }
printf("%u\n", max);
Try that with an array of values and watch what happenes i the debugger - you'll see what I mean.
 
Share this answer
 
v2
Comments
CPallini 18-Jan-21 2:15am    
5.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900