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

Write a C program that does the following, please follow the detailed instructio

ID: 3703109 • 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 more of the following any sequence of 1 or » 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 meet:s 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)f if (isalpha(c) return 1; c'-' llc- ' else return 0; Words in a passage of tex Your code will need to extract words from a 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 of text. To do this, you should take any For example, the string "Raspberry Pineapple" contains two words: "Raspberry" and "Pineapple". The string "Rasp###berry Pine123apple and "apple". Notice that any non-word character can be used to break up words, and that repeated sequences of non-word characters are all ignored while finding words. "contains four words: "Rasp", "berry", "Pine" For each of the three parts of this assignment, you will write a program which reads a text file input text.txt and extracts all of the words from the file. If you complete the parts in order, you should find that the solution for each part helps you complete the next part. Since our definition of a word is slightly different than usual (in particular, words do not include characters like digits and certain symbols), you cannot directly read words from the input file using fscanf. You will likely find that the easiest way to read words is to read the file character by character with fgetc and build word works. s as you go, but you are free to use any input method that

Explanation / Answer

#include<stdio.h>
#include<stdlib.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];
int c;
// To store number of word read from file
int wordCount;
// 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))
printf(" %s", word[c]);
}// End of for loop
}// End of main function

Sample Output:

grapefruit
straw
raspberry
blue
berry
pine
apple

input_txt.txt file contents

Raspberry Grapefruit Straw berry raspBERRY

Blue$berry apple Pine_apple raspberry

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote