Attached is what I have done so far in the c programming language. no pseudocode
ID: 3700611 • Letter: A
Question
Attached is what I have done so far in the c programming language. no pseudocode is required. I am having trouble figuring out how to open a given text file, reading the text file, and then closing the text file all while obtaining the names from the file. And then the actual part of comparing the strings of names. I know it must be done in a way that when after you compare it gives you a value of +1 or -1. compstr(string1,string2)
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: compareStr input character pointers: sl, s2 (i.e., const char *sl, const char *s2) output 0: the string in sl is the same as that in s2. CS1412 Programming Principles II, Spring 2018, TTU O Y. Zhang -1: the string in slis less than that in s2. +1: the string in sl is greater than that in s2. You have to write the pseudocode for this function too. You are NOT allowed to use any other functions in your pseudocode for compareStr.Explanation / Answer
#include<stdio.h>
#include<stdlib.h>
#define FNAME "name.txt"
// Function to compare two strings
// return +1 if string s1 is greater than the string s2
// return -1 if string s1 is less than the string s2
// return 0 if string s1 is equals to the string s2
int compareStr(char *s1, char *s2)
{
// Loop variable
int i = 0;
// Loops till string s1 i index position value is equals to the s2 i index position value
// and string i index position value is not null
while (s1[i] == s2[i] && s1[i] != '')
// Increase the counter by one
i++;
// Checks if string s1 i index position value is greater than the string s2 i index position value then return +1
if (s1[i] > s2[i])
return 1;
// Otherwise checks if string s1 i index position value is less than the string s2 i index position value then return -1
else if (s1[i] < s2[i])
return -1;
// Otherwise string s1 equals to string s2 return zero
else
return 0;
}// End of function
// Function to copy the contents of string s2 to string s1
void copyString(char *s1, char *s2)
{
// Loop variable
int i = 0;
// Loops till string s2 is not null
while(s2[i] != '')
{
// Copies each index position character of string s1 to string s2
s1[i] = s2[i];
// Increase the counter by one
i++;
}// End of while loop
// Assigns null at the end position
s1[i] = '';
}// End of function
// main function definition
int main()
{
// File pointer declared
FILE *fp;
// To store the names read from file
char name[50][100];
// To store the biggest last name
char biggestLastname[100];
// Loop variable
int i = 0, c;
// Opens the file for reading
fp = fopen(FNAME, "r");
// Checks if the file cannot be opened display error message and stop
if(!fp)
{
printf("ERROR: The file %s cannot be opened", FNAME);
exit(0);
}// End of if condition
// Loops till end of the file
while(!feof(fp))
{
// Reads data and stores in character matrix i index position
fscanf(fp, "%s", &name[i]);
// Increase the counter by one
i++;
}// End of while loop
// Displays the names read from file
for(c = 0; c < i; c += 2)
// Displays first and last name
printf(" First Name: %s Last Name: %s", name[c], name[c + 1]);
// Calls the function to copy the last name of the first person as biggest one
copyString(biggestLastname, name[1]);
// Loops through all the names
// i / 2 is taken because each person contains 2 name (first name and last name)
// Suppose the file contains 4 persons then 8 names (first name and last name)
for(c = 1; c < i/2; c++)
{
// Calls the function to compare the name
// If earlier biggest name is less than the current person last name then
// Update the current person last name as the biggest last name
// c * 2 + 1 for extracting only last name
// For second person last name: 2 * 2 + 1 = 5th index position will contain the last name
if(compareStr(biggestLastname, name[c * 2 + 1]) < 0)
copyString(biggestLastname, name[c * 2 + 1]);
}// End of for loop
// Displays the biggest last name
printf(" Biggest last name: %s", biggestLastname);
}// End of main function
Sample Output:
First Name: Allen Last Name: Turing
First Name: John Last Name: McCarthy
First Name: Dennis Last Name: Ritchie
First Name: Brian Last Name: Kernighan
Biggest last name: Turing
name.txt file contents
Allen Turing
John McCarthy
Dennis Ritchie
Brian Kernighan
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.