Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. Consider a text file. Each line of the file has the following form: firstName

ID: 3739997 • Letter: 1

Question

1. Consider a text file. Each line of the file has the following form: firstName lastName An example of the file, with name example.txt, is Allen Turing John McCarthy Dennis Ritchie Brian Kernighan Your program will ask a user to provide a name of a file with the format above. Your program print to the screen the full name whose last name is the greatest in dictionary order. One example execution of your program is Please input the file name: example.txt The full name whose last name is the greatest in the file is Allen Turing. You can assume NO two last names are the same in the file. (You are welcome not to use the assumption. Without that assumption, it will be more challenging and you need to give your tie breaker criteria when two full names share the same greatest last name When you compare two strings, DO NOT use the library function. Use the following one: Function name: compareS input character pointers: s 1, s2 (i.e., const char *si, const char *s2) output 0: the string in s1 is the same as that in s2.

Explanation / Answer

Solution:

code:

#include <stdio.h>
#include <stdlib.h>
#include<string.h>

#define LENGTH_MAX 100

//Function prototype
int compareStr(const char *str1, const char *str2);

//Main function
int main()
{
int start = 0;

//Declaring char arrays
char fileName[LENGTH_MAX];
char firstName[LENGTH_MAX], lastName[LENGTH_MAX];
char greaterFirst[LENGTH_MAX], greaterLast[LENGTH_MAX];

//File pointer
FILE *filePointer;

//Reading file name
printf(" Enter the file name: ");
gets_s(fileName, sizeof(fileName));

//Opening file in read mode
filePointer = fopen(fileName, "r");

//Checking for existence of file
if(!filePointer)
{
//Printing error
printf(" Error!!! Opening file... ");
return;
}

//Reading data from file
while( !feof(filePointer) )
{
//Reading names
fscanf(filePointer, "%s %s", firstName, lastName);

//For first time
if(start == 0)
{
//Initialize values greaterFirst and greaterLast
strcpy(greaterFirst, firstName);
strcpy(greaterLast, lastName);

start = 1;
}
else
{
//Comparing strings
if(compareStr(lastName, greaterLast) > 0)
{
//If greater last name is found, update
strcpy(greaterFirst, firstName);
strcpy(greaterLast, lastName);
}
}
}

//Printing full name
printf(" The full name whose last name is the greatest in the file is %s %s ", greaterFirst, greaterLast);

//Closing file
fclose(filePointer);

return 0;
}

//Function that compares two strings
int compareStr(const char *str1, const char *str2)
{
int i = 0;

//Iterating over strings
while (str1[i] == str2[i] && str1[i] != '')
i++;

//String 1 is greater than string 2
if (str1[i] > str2[i])
return 1;

//String 1 is less than string 2
else if (str1[i] < str2[i])
return -1;

//Both are same
else
return 0;
}

I hope this helps if you find any problem. Please comment below. Don't forget to give a thumbs up if you liked it. :)