Write a program that reads a sequence of words from an input file, sorts the wor
ID: 3599953 • Letter: W
Question
Write a program that reads a sequence of words from an input file, sorts the words in alphabetical order and then outputs them on the screen. For this exercise a word is a sequence of lower case letters. Note that the alphabetical order is consistent with the increasing order of the integer values of the characters. To read the data from the input file your program invokes the function read_words ( with prototype char **read_words (const char *input_-filename, int *nPtr) Note that input_filename is a string representing the name of the input file. This function has to store the words in an array of strings. The memory for the array of strings and for each string has to be allocated dynamically (i.e., using mallocO or calloc()). Do not allocate more memory then necessary for the array of strings and for the individual words. The input file contains a positive integer representing the number of words, on the first line. Then the words follow one per line. Function read_words O has to store the number of words in the variable pointed to by nPtr. Additionally, the function returns a pointer to the beginning of the array of strings that was dynamically allocated Next your program has to invoke the function sort_wordsO and next invoke function output_wordsO. Function sort_words has to sort the words in the array of strings in alphabetical order using the sorting algorithm known as "insertion sort". You have to determine the appropriate prototvpe for this function. Include a description of the sorting algorithm in a comment at the beginning of the function. Additionally, you have to write your own function to compare alphabetically two words. Function output_wordsO has to print all words on the screen in the order they appear in the array of strings, one word per line. Determine the appropriate prototype for this function Write another function sort2_wordsO which uses a different sorting algorithm (of your choice) to sort the words alphabetically. Include a description of the sorting algorithm in a comment at the beginning of the function. Test the function For this exercise you are allowed to use from the standard string processing library only the functions strlenO and strcpyO. The only other C standard library functions that you are allowed to use are for input/output, for opening a file, and for dynamic memory allocation.Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <sys/types.h>
bool sort2_words(char * word1,char * word2){
//length of two words
int len1 = strlen(word1);
int len2 = strlen(word2);
//to store minimum length of the two words
int min;
//find length of minimum word
if(len1>len2){
min = len2;
}
else{
min = len1;
}
int i;
for(i=0;i<len1;i++){
if(word1[i] == word2[i]){
continue;
}
else if(word1[i] > word2[i]){
return true;
}
else{
return false;
}
}
return false;
}
void sort_words(char **wordsarray,int arraySize){
//insertion sort for sorting array of words
int i, j;
char *key;
for (i = 1; i < arraySize; i++){
strcpy(key,wordsarray[i]);
j = i-1;
/* Move elements of wordsarray[0..i-1], that are
greater than key, to one position ahead
of their current position */
while (j >= 0 && sort2_words(wordsarray[j],key))
{
strcpy(wordsarray[j+1] ,wordsarray[j]);
j = j-1;
}
strcpy(wordsarray[j+1] , key);
}
return;
}
void output_words(char **wordsarray,int arraySize){
int i;
//iterate thw words array, and print each word in a line
for(i=0;i<arraySize;i++){
//printing the individual word
printf("%s ",wordsarray[i]);
}
//printing a new line
printf(" ");
}
char ** read_words(const char *input_filename, int *nPtr){
char * line = NULL;
size_t len = 0;
ssize_t read;
//read the file
File *fp = fopen(input_filename,"r");
//exit if failure
if(fp == NULL){
exit(EXIT_FAILURE);
}
// read the first line and store the number to *nPtr
if((read = getline(&line, &len, fp)) != -1){
*nPtr = atoi(line);
}
// create words array of the required size
char *wordsarray[*nPtr];
int i=0;
while((read = getline(&line, &len, fp)) != -1){
//copy the word read to the words array
strcpy(wordsarray[i],line);
i++;
}
fclose(fp);
return wordsarray;
}
int main(){
//words array
char **wordsarray;
// number of words in the file
int num_words;
//file name to read
char *input_filename = "words.txt";
//call the read_words function and store the length in num_words and the words array in words_array
wordsarray = read_words(input_filename , &num_words);
//sort the words
sort_words(wordsarray , num_words);
//output the words
output_words(wordsarray , num_words);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.