Write a C program that does the following, please follow the detailed instructio
ID: 3705765 • Letter: W
Question
Write a C program that does the following, please follow the detailed instructions throughly, it is only one question, and the instructions make it easy for you :
Overview This assignment covers strings, structures (potentially, depending on your solution method), file I/O and problem solving techniques. The goal of the assignment is to write C programs which can parse and analyze English text, and the three parts of the assignment represent different milestones in producing a program to analyze the word distribution in a passage of text. Definition of a word Since this assignment requires reading words from a passage of text, it is important to have a rigorous defintion of what constitutes a word. For this assignment, a word is any sequence of 1 or more of the following. » Alphabetical characters (either uppercase or lowercase » The hyphen character - » The apostrophe character' For extra clarity, the function is_word_character below returns 1 if the provided character meets the definition of a word character above and returns 0 otherwise. You don't have to use this function in your code; it is only provided as a source of context int is word character(char c)1 if (isalpha(c) Il c == '-' ll c-''') return 1; else return 0; Words in a passage of text Your code will need to extract words from a passage of text. To do this, you should take any consecutive set of word characters (as defined above) as a word and use any non-word characters (like spaces, newlines, digits or symbols other than hyphens or apostrophes) as a separator.Explanation / Answer
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAXC 100 // Maximum column of character matrix
#define MAXR 100 // Maximum row of character matrix
// Function to return q if the character is '_' or ''' or alphabet otherwise returns zero
int is_word_character(char c)
{
// Checks if the character is '_' or ''' or alphabet then returns one
if(isalpha(c) || c == '_' || c == ''')
return 1;
// Otherwise returns zero
else
return 0;
}// End of function
// Function to return one if the character is ' ' or ' ' or space or not a alphabet otherwise return zero
int is_new_word(char c)
{
// Checks if the character is ' ' or ' ' or space or not a alphabet returns one
if(c == ' ' || c == ' ' || c == ' ' || !isalpha(c))
return 1;
// Otherwise returns zero
else
return 0;
}// End of function
// Function to read file and stores the word in character matrix
int readFile(char word[MAXR][MAXC])
{
// For word counter and character counter
int wordC, charC;
// To store character
char ch;
// Loop variable
int c;
// File pointer declared
FILE *fRPtr;
// Opens the file for reading and checks whether it can be opened or not
if ((fRPtr = fopen("input_txt.txt", "r")) == NULL)
{
// If unable to open shows error message
printf("Error! in opening file for reading");
// Program exits if the file pointer returns NULL.
exit(1);
}// End of if condition
// Initializes the counter to zero
wordC = charC = 0;
// Loops till end of file
while(!feof(fRPtr))
{
// Extracts a character and stores it in ch
fscanf(fRPtr, "%c", &ch);
// Calls the function to check whether it is a new word
if(is_new_word(ch))
{
// Assigns null character at the end
word[wordC][charC] = '';
// Increase the word counter by one
wordC++;
// Reset the character counter to zero for next word
charC = 0;
}// End of if condition
// Otherwise, calls the function to checks if the character is a word character
else if(is_word_character(ch))
{
// Assigns the character at row index position wordC and column index position charC
word[wordC][charC] = ch;
// Increase the character counter by one
charC++;
}// End of else if condition
}// End of while condition
// Closes the file
fclose(fRPtr);
// Returns number of words
return wordC;
}// End of function
// Function to return one if the word is duplicate.
// char data[] -> To check the word for duplicate
// char mat[][MAXC] -> contains all the words
// int len -> number of words
// int position -> current position of the word in matrix
int findDuplicate(char data[], char mat[][MAXC], int len, int position)
{
// Loop variable
int c;
// Initial status is zero
int status = 0;
// Loops from next position from the current position till number of words
for(c = position+1; c < len; c++)
{
// Compares the word with each next words in the matrix is equal or not
if(strcmp(data, mat[c]) == 0)
{
// If equal then reset the status to one
status = 1;
// Come out of the loop
break;
}// End of if condition
}// End of for loop
// Returns the found status
return status;
}// End of function
// Function to convert each word to lower case
// char mat[][MAXC] -> matrix contains all the words
// int len -> number of words
void lower_string(char mat[][MAXC], int len)
{
// Loop variable
int c, d;
// Loops till all the words
for(c = 0; c < len; c++)
{
// Loops till end of current word
for(d = 0; mat[c][d] != ''; d++)
{
// Checks if the character is between 'A' to 'Z'
if ( mat[c][d] >= 'A' && mat[c][d] <= 'Z' )
{
// Subtract 32 from it to convert it into lower chase
mat[c][d] = mat[c][d] + 32;
}// End of if condition
}// End of inner for loop
}// End of outer for loop
}// End of function
// main function definition
int main()
{
// To store words read from file
char word[MAXR][MAXC];
// To store shortest words
char shortestWord[MAXR][MAXC];
// To store longest words
char longestWord[MAXR][MAXC];
// Loop variable
int c;
// To store number of word read from file and counters and position variable
int currentWordLen, wordCount, uniqueWordCount, longestWordLen, longestPos, longestWordCounter, shortestWordLen, shortestPos, shortestWordCounter;
// Initializes counters and position variable
uniqueWordCount = longestWordCounter = shortestWordCounter = 0 ;
longestWordLen = longestPos = shortestPos = -1;
shortestWordLen = MAXC;
// calls the function to read file contents and store it in character matrix
wordCount = readFile(word);
// Calls the function to convert to lower case
lower_string(word, wordCount);
// Loops till number of words
for(c = 0; c < wordCount; c++)
{
// Calls the function to check duplicate. If not duplicate then display the word
if(!findDuplicate(word[c], word, wordCount, c))
{
// Counts the length of the current word
for(currentWordLen = 0; word[c][currentWordLen] != ''; currentWordLen++)
; // Do nothing statement
// Checks if the current word length is greater than the earlier longest word length
if(currentWordLen > longestWordLen)
{
// Update the longest word length to current word length
longestWordLen = currentWordLen;
// Stores the position of the longest word length as loop variable value c
longestPos = c;
}// End of if condition
// Checks if the current word length is less than the earlier shortest word length
if(currentWordLen < shortestWordLen)
{
// Update the shortest word length to current word length
shortestWordLen = currentWordLen;
// Stores the position of the shortest word length as loop variable value c
shortestPos = c;
}// End of if condition
// Increase unique counter by one
uniqueWordCount++;
}// End of if condition
}// End of for loop
// Loops till number of words
for(c = 0; c < wordCount; c++)
{
// Calls the function to check duplicate. If not duplicate then display the word
if(!findDuplicate(word[c], word, wordCount, c))
{
// Counts the length of the current word
for(currentWordLen = 0; word[c][currentWordLen] != ''; currentWordLen++)
;// Do nothing statement
// Checks if the longest word length is equals to the current word length
if(longestWordLen == currentWordLen)
{
// Copy the current word to longest word matrix longestWordCounter index position
strcpy(longestWord[longestWordCounter], word[c]);
// Increases the longest word counter by one
longestWordCounter++;
}// End of if condition
// Checks if the shortest word length is equals to the current word length
if(shortestWordLen == currentWordLen)
{
// Copy the current word to shortest word matrix shortestWordCounter index position
strcpy(shortestWord[shortestWordCounter], word[c]);
// Increases the shortest word counter by one
shortestWordCounter++;
}// End of if condition
}// End of if condition
}// End of for loop
// Displays the information
printf(" Total Words: %d", wordCount);
printf(" Unique words: %d", uniqueWordCount);
for(c = 0; c < longestWordCounter; c++)
printf(" Longest words: %s ", longestWord[c]);
for(c = 0; c < shortestWordCounter; c++)
printf(" Shortest words: %s ", shortestWord[c]);
}// End of main function
Sample output:
Total Words: 10
Unique words: 7
Longest words: grapefruit
Shortest words: blue
Shortest words: pine
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.